calling a js file in wordpress

2019-09-19 07:04发布

I have a wordpress site and I'm trying to load a .js file to it. For some reason I can't get it to work.

The js file is at http://www.leadcomm.net/pricequotejs.js

and I put this in the footer:

 <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>

Any ideas on what I'm doing wrong?

3条回答
何必那么认真
2楼-- · 2019-09-19 07:31

Is there any particular reason you can't use a standard HTML script tag?

<head>
    ...
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/pricequotejs.js"></script>
</head>
查看更多
该账号已被封号
3楼-- · 2019-09-19 07:39

this is not the proper manner to use a js file in wordpress. first you need to save your js file in a folder named js on you theme folder. then open your header.php file and use this add this line :

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

this will load your js file to wordpress website. Happy coding!

查看更多
Root(大扎)
4楼-- · 2019-09-19 07:41

I left the document onload function. silly me...

查看更多
登录 后发表回答