Social Icons

Pages

How nicely to escape/embed jQuery in perl?


In perl you might have inline jQuery like this:

 # DOES NOT WORK
 print <<EOF;
 <script>
 $(document).ready(function() {
         $('#element_id').show();
 });
 </script>
 EOF

But this doesn't work because perl will interpret the $ as the start of
a variable name and try to interpret it.  So you can escape the dollar
symbols from perl like this:

 # WORKS BUT UGLY
 print <<EOF;
 <script>
 \$(document).ready(function() {
         \$('#element_id').show();
 });
 </script>
 EOF

That's ugly.
But, the dollar is just an alias for "jQuery" so you can do this:

 # WORKS AND PRETTY
 print <<EOF;
 <script>
 jQuery(document).ready(function() {
         jQuery('#element_id').show();
 });
 </script>
 EOF

Which works and is clearer.

Няма коментари:

Публикуване на коментар