0

我需要将这段特定的 JS 代码添加到 Wordpress 支持的网站区域。

    <script type="text/javascript">
    var myDate = new Date();
    var myStamp = ""+myDate.getDate()+myDate.getMonth()+myDate.getFullYear()+myDate.getHours()+myDate.getMinutes();
    document.write('<script type="text/javascript" src="https://ajaxgeo.cartrawler.com/cartrawlerabe/abe/js/abeSVNInfo.js?' + myStamp + '"><\/script>');
    </script>
    <script type="text/javascript">
    document.write('<script type="text/javascript" src="https://ajaxgeo.cartrawler.com/cartrawlerabe/abe/js/ct_abe.js?'+CARTRAWLER.SVNInfo.revision+'"><\/script>');
    </script>
    <script type="text/javascript">
    //<![CDATA[
    var theForm = document.forms['form'];
    if (!theForm) {
        theForm = document.form;
    }
    function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
        }
    }
    //]]>
    </script>

实现这一目标的最佳方法是什么?

4

3 回答 3

1

In this situation I would say using the WordPress wp_enqueue_script function works best.

Start by putting the JavaScript into a seperate document (ie. cartrawler.js). Put this JavaScript document in your theme (preferably in a /js folder). After that you will tell your theme to use the JavaScript from that folder. The benefit of this method is that it is easier to control when and where the JavaScript should be loaded (like only in the header of the home paga) and it allows for better compatibility for caching with plugins like (Better) WP Minify.

This is how it works:

Make a seperate JavaScript document and store it within your theme folder (ie. theme/js/cartrawler.js):

var myDate = new Date();
var myStamp = ""+myDate.getDate()+myDate.getMonth()+myDate.getFullYear()+myDate.getHours()+myDate.getMinutes();
document.write('<script type="text/javascript" src="https://ajaxgeo.cartrawler.com/cartrawlerabe/abe/js/abeSVNInfo.js?' + myStamp + '"><\/script>');
document.write('<script type="text/javascript" src="https://ajaxgeo.cartrawler.com/cartrawlerabe/abe/js/ct_abe.js?'+CARTRAWLER.SVNInfo.revision+'"><\/script>');

var theForm = document.forms['form'];
if (!theForm) {
    theForm = document.form;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

After you have made the JavaScript document you can add this to your function.php document:

// Enqueue JavaScripts
function theme_scripts() {
    wp_enqueue_script('cartrawler', get_template_directory_uri() . '/js/cartrawler.js', array(), '20120802', '1');
}
add_action('wp_enqueue_scripts', 'theme_scripts');

What we do when calling the JavaScript is the following: We give the JavaScript a name (cartrawler) and tell the function where the source can be found. Afterwards we define any dependencies, luckily we don't have any. After that we add a version number (20120802) and tell the function to load the JavaScript in the footer. It doesn't need to be in the header afterall (thumbsup @Lixus).

Read more about using the wp_enqueue_script function here: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Note: Make sure your header.php uses the wp_header() function and that your footer.php uses the wp_footer() function.

于 2012-08-02T12:09:25.923 回答
1

你是说前端?

header.php在使用的模板中通常是正确的地方。

于 2012-08-02T07:25:32.100 回答
0

在 Wordpress 后端,转到外观 -> 编辑器,然后在右侧菜单 (footer.php) 中选择页脚模板。

然后在标签之前添加您的代码</body>

编辑:我认为最好将此代码添加到页脚,因为它不会等待文档加载。

于 2012-08-02T07:27:01.497 回答