开发者

How can I avoid jQuery scripts being loaded multiple times?

On my site, I load jQuery UI script using the following code:

wp_enqueue_script('nf-jquery-ui','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js')

In my plugin I use the following code:

wp_enqueue_script('wp_filebrowser-jqueryui','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js')

The result is that both these lines are added to my header - and loading a script twice is not very good.

Yes, I know I can remove开发者_Go百科 it from either my plugin or my site. But the point is that anyone can download my plugin, and they might already be loading the jQuery UI script.

How can I avoid having a script being added twice?


You could deregister the first script by putting this line in your plugin...

wp_deregister_script( 'nf-jquery-ui');

before you call your 'enqueue script'

wp_enqueue_script('wp_filebrowser-jqueryui','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js')

This should remove the first jQuery call on all pages that use your plugin.


The WP API states the first param is a handler. That one is probably used so a script is only inserted once (although I can not say for sure).

It also defines handlers for jQuery and jQuery UI which you should probably use. Those handlers are for included scripts and jQuery as well as jQuery-ui is listed there. If you want to update consider passing the version to the wp_-function or just replacing the jQuery script file that’s already in use / packaged with wp.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜