且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在jquery.js加载到WordPress站点后加载javascript代码

更新时间:2023-12-01 23:03:58

根据wordpress函数引用:

As per wordpress function reference:


使用wp_enqueue_script将安全性和推荐的方法添加到WordPress生成的页面()。此函数包括脚本(如果尚未包含),并安全地处理依赖项。

The safe and recommended method of adding JavaScript to a WordPress generated page is by using wp_enqueue_script(). This function includes the script if it hasn't already been included, and safely handles dependencies.

如果要保留代码干净你可能会重新思考你的js是如何组织的。
我使用wordpress的方法是(通常)为所有初始化和小脚本保留一个scripts.js,为插件保留单独的文件。但是一切都取决于你有多大和多少文件 - 你想避免太多的http请求和难以管理的文件。

If you want to keep your "code clean" you might rethink how your js is organised. My approach with wordpress is to (usually) keep a scripts.js for all initialisation and small scripts and separate file(s) for plugins. But everything depends how big and how many files you have - you want to avoid too many http requests and files that are hard to manage.

同样,wp_enqueue_script让你放置页脚中的脚本。
http://codex.wordpress.org/Function_Reference/wp_enqueue_script

As well, wp_enqueue_script lets you place the scripts in the footer. http://codex.wordpress.org/Function_Reference/wp_enqueue_script