您可以禁用腳本元素。 有幾種方法:
您可以指定與其他腳本類型不同的腳本類型。 這是對我有用的一個:
//loop through all script tags on page
$('script').each(function(){
var scripthtml = $(this).html();
$(this).replaceWith('<script type="text/gzip">' + scripthtml + '</script>');
});
所有官方腳本類型都可以在這里找到: iana.org
第二種方法只是一個簡單的 if 語句:
//loop through all script tags on page
$('script').each(function(){
var scripthtml = $(this).html();
$(this).replaceWith('<script>if (1==0){' + scripthtml + '}</script>');
});
if 語句將始終為 false,因此腳本標記內的任何內容都不會執行。 但是,腳本標記內的所有函數() 都將有效。
繼承人的javascript 相當於replaceWith:
//get script and html
var yourscripttag = document.getElementById('yourscripttagID');
var scripthtml = 'if (1==0){' + yourscripttag.innerHTML + '}';
//remove script
yourscripttag.remove();
//create new script element
var newscript=document.createElement('script');
newscript.type='text/javascript';
//insert html in new script tag
newscript.appendChild(document.createTextNode(scripthtml));
//insert new script tag into head
document.getElementsByTagName('head').item(0).appendChild(newscript);