我如何只在选定的页面上运行我的.js文件?

时间:2018-03-17 17:07:25

标签: javascript

我有一个.js文件我只想在我的网站上超过1000页的30页上运行。这些页面在URL的末尾有一些明确的标识符,例如“catalog / products / 60394136 /”每页都会更改最后一个数字。

2 个答案:

答案 0 :(得分:0)

您可以在php中创建一个数组:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr>
<div id="x"></div>
<hr>

并将其放在HTML文件中:

<?php

$allowed = array('60394136','Other values');
$import = (in_array($id,$allowed)) ? true : false;

?>

如果你想在将来添加更多id,可以从数据库中获取数组,并通过将它们添加到数据库中来添加更多id

答案 1 :(得分:0)

建议 Window.location.href 在Js中进行比较。

它返回当前标签打开的确切网址。

你可以这样做

var tabURl = window.location;
var arrId = tabURl.split("/");
if(arrId[identifier Index] == "page ID"){
`enter code here`
}

您可以查看相同的here