Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Perl, JavaScript and quoting/escaping

by isync (Hermit)
on Sep 23, 2008 at 18:11 UTC ( [id://713268]=note: print w/replies, xml ) Need Help??


in reply to Perl, JavaScript and quoting/escaping

Actually I solved it half-way: The problem is less the quoting on the perl side, but what ends up in the html output:

\" becomes " - so inside the href="" we can use the single-quote ' to differentiate things. And if in these single-quotes values need to be presented we double quote \\' so the \' ends up in the html, like example.jsfunction('<a href=\'\'></a>')

But still I am in trouble if I need to add yet another nested layer of quoted values, for example example.jsfunction('<a href=\'\' onclick=\\'alert('test')\\'></a>') - what shoudl I do with the quotes around test? Any ideas?

Update:
I've found out that, at least in Firefox, using entities works: example.jsfunction('<a href=\'\' onclick=\\'alert(&quot;test&quot;)\\'></a>') - but is that portable?

Replies are listed 'Best First'.
Re^2: Perl, JavaScript and quoting/escaping
by brycen (Monk) on Sep 23, 2008 at 18:17 UTC
    As the prior poster said: use perl's "bring your own quote characters" to rescue you. Leave ' and " for javascript's feeble mind.
Re^2: Perl, JavaScript and quoting/escaping
by Anonymous Monk on Sep 23, 2008 at 18:31 UTC
    The entity is &quot;.
      oops - a simple typo in the post, code was ok.
Re^2: Perl, JavaScript and quoting/escaping
by Anonymous Monk on Sep 23, 2008 at 19:15 UTC
    Generally, as long as you keep using the same type of quote, you double the \'s for each nested level, so try:
    example.jsfunction('<a href=\'\' onclick=\\'alert(\\\\'test\\\\')\\'>< +/a>')

      That would only work for if all of JS, HTML and Perl used "\" to escape. HTML doesn't.

      Assuming you had the right number of slashes — you don't — you're code results in the following HTML:

      <a href='' onclick='alert(\' <- Nice JS! test\')' <- eh? ></a>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://713268]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found