您可以禁用脚本元素。 有几种方法:
您可以指定与其他脚本类型不同的脚本类型。 这是对我有用的一个:
//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);