我想使用通俗易懂的语言进行检查,如果页面中某处有某个脚本被两次或更多次调用。 例如:
<script src="myscripts.js"></script>
<script src="myscripts.js"></script>
是否可以使用液体或应该使用JavaScript进行验证?
答案 0 :(得分:0)
我不确定液体,但是如果您想使用JS路线,则可以这样做:
//locate all `<script>` tags and save the elements into an array
var scriptTags = [];
document.querySelectorAll('script').forEach(function(tag) {
scriptTags.push(tag)
});
//Put just the URLs of the script tags into an array
var scriptUrls = scriptTags.map(function(tag) {
return tag.src;
});
//Get a list of any URL that appears more than once
var duplicateUrls = scriptUrls.filter(function(url, i) {
return scriptUrls.indexOf(url) != i;
});
console.log(duplicateUrls);
<script src="dupe.js"></script>
<script src="other1.js"></script>
<script src="dupe.js"></script>
<script src="other2.js"></script>
<script src="other3.js"></script>