我有:
<body class="home">
content
</body>
然后我有:
<script src="abs.js"></script>
<script src="abse.js"></script>
<script src="abses.js"></script>
我头上有这些脚本,它们正在所有页面上运行。如何仅在“ .home”页面上运行它们?
谢谢:-)
答案 0 :(得分:1)
HTML的答案是只将它们包含在首页的标题中,然后从其他页面中删除脚本
答案 1 :(得分:1)
您可以通过编写函数来解决问题,并通过检查URL是否为主页URL来加载脚本。检查以下代码,它将对您有所帮助。
注意:将网站网址从 / home / sys3028 / Desktop /
/* loading scripts */
loadingScripts();
/* written by christyram99 */
/* used to load scripts for specific page */
function loadingScripts() {
/* getting url of current page */
var url = window.location.pathname;
if (url == "/home/sys3028/Desktop/home.html") { /* checking whether the current page is home page or not */
var script = document.createElement('script');
script.src = "1.js";
} else if (url == "/home/sys3028/Desktop/login.html") { /* checking whether the current page is login page or not */
var script = document.createElement('script');
script.src = "2.js";
} else if (url == "/home/sys3028/Desktop/contact.html") { /* checking whether the current page is contact page or not */
var script = document.createElement('script');
script.src = "3.js";
}
}