Dave's Brain

Browse - programming tips - nice way to embed jquery in perl

Date: 2009oct28
Language: javaScript, perl

Q.  What's a nice way to embed jQuery in perl?

A.  In perl you might have inline jQuery like this:

	# DOES NOT WORK
	print <<EOF;
	<script>
	$(document).ready(function() {
        	$('#datepicker').datepicker();
	});
	</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() {
        	\$('#datepicker').datepicker();
	});
	</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('#datepicker').datepicker();
	});
	</script>
	EOF

Which works and is clearer.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.