2012-08-25 39 views
0

我有一個wordpress網站,我正試圖爲它加載一個.js文件。出於某種原因,我無法讓它工作。在wordpress中調用js文件

JS文件是在http://www.leadcomm.net/pricequotejs.js

,我把這個頁腳:

<script type="text/javascript"> 
if (window.location.href == 'http://www.leadcomm.net/cost-estimate/') {  
    var js = document.createElement('script'); 
    js.setAttribute("type","text/javascript"); 
    js.setAttribute("src", "pricequotejs.js"); 
    document.getElementsByTagName("head")[0].appendChild(js); 
} 
</script> 

什麼我做錯了任何想法?

回答

0

我離開文檔onload函數。傻我...

0

是否有任何特殊的原因,你不能使用標準的HTML腳本標記?

<head> 
    ... 
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/pricequotejs.js"></script> 
</head> 
1

這不是在wordpress中使用js文件的正確方式。 首先,您需要將您的js文件保存在您的主題文件夾中名爲js的文件夾中。然後打開你的header.php文件,並使用該加入這一行:

<script language="javascript" type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/your_file.js"></script> 

,這將載入您的js文件到WordPress網站。 快樂編碼!

+0

我怎樣才能讓它只運行一個頁面?您可以使用 – Finx

+0

在您的header.php文件中使用 <?php is_page('455') { }?> 使用此代碼。希望它有幫助。 – Gaurav

+0

如果你的頁面是主頁然後使用這個: <?php is_home(){}?> – Gaurav