开发者

using appendchild to conditionally load jQuery

开发者 https://www.devze.com 2023-03-13 15:42 出处:网络
I have a wordpress plugin to generate a flickr gallery.To avoid conflict with other jquery plugins, I only load jquery if it is not loaded already.But issue with this approach is that the colorbox doe

I have a wordpress plugin to generate a flickr gallery. To avoid conflict with other jquery plugins, I only load jquery if it is not loaded already. But issue with this approach is that the colorbox doesn't work if the jquery is loaded using this method. I don't see the cboxoverlay and colorbox elements in the body of the page.

This is my code -

if((typeof(jQuery) == 'undefined') || (jQuery.fn.jquery < '1.4.4')){
    var loadjQuery = document.createElement(\"script\");
    loadjQuery.setAttribute(\"src\",\"https://ajax.googleapi开发者_运维知识库s.com/ajax/libs/jquery/1.4.4/jquery.min.js\");
    var head = document.getElementsByTagName(\"head\")[0];
    head.appendChild(loadjQuery);
}

Any clue what can be done to fix both the issues?


You should be using the function enqueue_script.

"A safe way of adding javascripts to a WordPress generated page. Basically, include the script if it hasn't already been included, and load the one that WordPress ships."

Basically, this will allow you to include you scripts safely with WordPress doing the work.

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

If that is not enough detail let me know, and I can elaborate

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号