Wow! JS in HTML in JS in HTML in Perl!

(I've broken the lines for readability. It's not quite equivalent with the line breaks, so they should be removed in the final version.)

Start with the inner JavaScript:

this.function.test('')

Add the inner HTML/XHTML:

<tr id="someid"><td> <a href="" onclick=" this.function.test('') "></a> </td></tr>

Add the JavaScript:

this.element.insert(' <tr id="someid"><td> <a href="" onclick=" this.function.test(\'\') "></a> </td></tr> ')

Add the outer HTML/XHTML: (XHTML requires that "<" be encoded. I encoded ">" as well, though it's not required.)

<a href="" onclick=" this.element.insert(' &lt;tr id=&quot;someid&quot;&gt;&lt;td&gt; &lt;a href=&quot;&quot; onclick=&quot; this.function.test(\'\') &quot;&gt;&lt;/a&gt; &lt;/td&gt;&lt;/tr&gt; ') ">

Add the Perl:

print(" <a href=\"\" onclick=\" this.element.insert(' &lt;tr id=&quot;someid&quot;&gt;&lt;td&gt; &lt;a href=&quot;&quot; onclick=&quot; this.function.test(\\'\\') &quot;&gt;&lt;/a&gt; &lt;/td&gt;&lt;/tr&gt; ') \"> ");

For comparison purposes, here's what you had:

print(" <a href=\"\" onclick=\" this.element.insert(' <tr id=\"someid\"><td> <a href=\"\" onclick=\" this.function.test(\'\') \"></a> </td></tr> ') \" ");

Not even close. It's much better to do it programmatically:

use HTML::Entities qw( encode_entities ); sub text_to_ht_attr_val { my $s = @_ ? $_[0] : $_; $s = encode_entities($s); return qq{"$s"}; } sub text_to_js_lit { my $s = @_ ? $_[0] : $_; $s =~ s/\\/\\\\/g; $s =~ s/'/\\'/g; # ... return qq{'$s'}; } my $ht_to_insert = q{<tr id="someid"><td>} . q{<a href="" onclick="this.function.test('')"></a>} . q{</td></tr>}; my $js_onclick = 'this.element.insert(' . text_to_js_lit($ht_to_insert) . ')'; my $ht_dyn_ele = '<a href="" onclick=' . text_to_ht_attr_val($js_onclick) . '>'; print( "$ht_dyn_ele\n" );
<a href="" onclick=" this.element.insert(&#39; &lt;tr id=&quot;someid&quot;&gt;&lt;td&gt; &lt;a href=&quot;&quot; onclick=&quot; this.function.test(\&#39;\&#39;) &quot;&gt;&lt;/a&gt; &lt;/td&gt;&lt;/tr&gt; &#39;) ">

For comparison purposes, here's what I had:

<a href="" onclick=" this.element.insert(' &lt;tr id=&quot;someid&quot;&gt;&lt;td&gt; &lt;a href=&quot;&quot; onclick=&quot; this.function.test(\'\') &quot;&gt;&lt;/a&gt; &lt;/td&gt;&lt;/tr&gt; ') ">

(Note that the single quotes didn't need to be encoded, but encode_entities harmlessly encodes them as "&#39;".)


In reply to Re: Perl, JavaScript and quoting/escaping by ikegami
in thread Perl, JavaScript and quoting/escaping by isync

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.