Reputation: 2282
wordpress include jquery.js but at the end of the script they have this line:
jQuery.noConflict()
So, I am not able to use $(), but I need to output js in footer and that script uses $.
How to set $?
Is this valid to do in footer:
<script type="text/javascript">
if (typeof $ === "undefined") {
var $ = jQuery.noConflict();
}
</script>
Or this:
<script type="text/javascript">
if (typeof $ === "undefined") {
document.write('<script type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></'+'script>');
}
</script>
But in second case there would two of the same jquery.js included in two diferent places just the last one without line: jQuery.noConflict();
Upvotes: 0
Views: 1174
Reputation: 17734
Wordpress is merely being a good neighbor. As the API docs state — jQuery.noConflict()
does …
relinquish jQuery's control of the
$
variable.
Many libraries and frameworks are making use of $
and calling jQuery.noConflict()
after defining your script ensures that all jQuery variables are removed from the global scope and that your code won’t interfere with anyone else’s.
I prefer to define my jQuery code in a closure — like this I’m able to use $
as usual while not polluting the global namespace:
(function($) {
// Code using `$` ...
})(jQuery);
Upvotes: 2