已知在一个html中有若干javascript,现要求用某个javascript删除最后一个javascript,实例如下<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<script type="text/javascript">
document.write("js1\n");
</script>
<script type="text/javascript">
document.write("js2\n");
</script>
<script type="text/javascript">
document.write("js3\n");
</script>
<script type="text/javascript">
document.write("js4\n");
</script>
<script type="text/javascript">
/*删除最后一个javascript,使之不执行,页面上不会出现 js5 */
</script>
<script type="text/javascript" id="target">
document.write("js5\n");
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<script type="text/javascript">
document.write("js1\n");
</script>
<script type="text/javascript">
document.write("js2\n");
</script>
<script type="text/javascript">
document.write("js3\n");
</script>
<script type="text/javascript">
document.write("js4\n");
</script>
<script type="text/javascript">
/*删除最后一个javascript,使之不执行,页面上不会出现 js5 */
</script>
<script type="text/javascript" id="target">
document.write("js5\n");
</script>
</body>
</html>
document.write("js5\n");
}
<!DOCTYPE html>
<html>
<body>
<script id="sc">
function foo() {};
</script>
<script>
var sc = document.getElementById("sc");
document.body.removeChild(sc);
</script>
<script>
var scripts = document.getElementsByTagName("script");
alert(scripts.length); //2
alert(typeof foo); //function
</script>
<!--script>
var sc = document.getElementById("sc");
document.body.removeChild(sc);
</script>
<script id="sc">
function foo() {};
</script>
<script>
var scripts = document.getElementsByTagName("script");
alert(scripts.length); //3
alert(typeof foo); //function
</script-->
</body>
</html>
可以移除已经加载的script节点,但是其函数声明依旧存在,但是无法移除未加载的script节点,因为脚本是按顺序加载的。
就像楼上说的,干脆 采用条件语句
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<script type="text/javascript">
document.write("js1\n");
</script>
<script type="text/javascript">
document.write("js2\n");
</script>
<script type="text/javascript">
document.write("js3\n");
</script>
<script type="text/javascript">
document.write("js4\n");
</script>
<script type="text/javascript">
//实现的原理是重写目标代码段里调用的第一个函数,从里面throw一个异常出来,终结掉整个代码段的执行
//首先保存write函数,以便执行完以后恢复。如果你不需要恢复,那么就不需要保存
var temp=document.write;
document.write=function(){throw 1;};
</script>
<script type="text/javascript" id="target">
document.write("js5\n");
alert(1);
</script>
<script type="text/javascript">
//恢复write函数,以便其他调用
document.write=temp;
</script>
<script type="text/javascript">
document.write("js6\n");
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<script type="text/javascript">
document.write("js1\n");
</script>
<script type="text/javascript">
document.write("js2\n");
</script>
<script type="text/javascript">
document.write("js3\n");
</script>
<script type="text/javascript">
document.write("js4\n");
</script>
<script type="text/javascript">
<!--
得到二楼的启发
你可以在这里写插入注释的js,在本代码块的后面紧跟着注释符,因为这时本代码块已经加载执行,下一个script块还没有加载,把注释符插到自己的后面
然后注册onload事件,在文档全部加载完后插入注释结束符
-->
</script>
<!-- 这里注释掉后面的所有代码,后面的依然会继续加载,但不执行
<script type="text/javascript" id="target">
document.write("js5\n");
</script>这里在这个文档加载完后插入注释结束符-->
</body>
</html>
这样不知道行不行,没测试过
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<script type="text/javascript">
document.write("js1\n");
</script>
<script type="text/javascript">
document.write("js2\n");
</script>
<script type="text/javascript">
document.write("js3\n");
</script>
<script type="text/javascript">
document.write("js4\n");
</script>
<script type="text/javascript">
document.write("</body></html><!--");//这句可以注释掉后面
</script>
<script type="text/javascript" id="target">
document.write("js5\n");
</script>
</body>
</html>