1.以下代码

a) </head></script>顺序写反了,导致不能认识到有script

b)但还是执行不正常,为啥呢?

A:这里创建了东西,但是没有添加进去!


为啥以下网页不能正常执行呢?_javascript为啥以下网页不能正常执行呢?_html_02

1 <head>  2 <script type="text/javascript">  3   4 function f()  5 {  6  var c=document.createElement('a');  7   c.setAttribute("href","d[2].value");  8   c.setAttribute("name","a1");  9   c.setAttribute("class","b"); 10   c.innerHTML = "d[1].value"; 11 } 12 </head> 13  14 </script>   15  16 <body onload="f()"> 17  18 </body>

View Code


2. 发现以下代码img src="http://www.w3schools.com/jsref/w3javascript.gif" 里面的图片地址无效的话就会不能执行

为啥以下网页不能正常执行呢?_javascript为啥以下网页不能正常执行呢?_html_02

<!DOCTYPE html> <html> <body onload="loadImage()" >  <img src="http://www.w3schools.com/jsref/w3javascript.gif" onload="loadImage()" width="100" height="132">  <script> function loadImage() {     alert("Image is loaded"); } </script>  </body> </html>

View Code


3. 以下代码能执行,但是如果把document.getElementById("myDIV").appendChild(c);改成document.appendChild(c);就会出错!

为啥以下网页不能正常执行呢?_javascript为啥以下网页不能正常执行呢?_html_02

1 <head>  2 <script type="text/javascript">  3   4 function f()  5 {  6  var c=document.createElement('a');  7   c.setAttribute("href","http://www.qq.com");  8   c.setAttribute("name","a1");  9   c.setAttribute("class","b"); 10   c.innerHTML = "http://www.qq.com"; 11    12   document.getElementById("myDIV").appendChild(c); 13 } 14 </script>   15  16 </head> 17  18  19  20 <body onload="f()"> 21 <div id="myDIV"/> 22 </body>

View Code


4.以下GM代码

1)直接执行,能够跳出两个对话框,并且能执行oldfunc,说明这段代码的确覆盖到了原有的代码执行了 (怎样执行的呢?会比onload还早?)

用firebug看到html代码为

为啥以下网页不能正常执行呢?_javascript为啥以下网页不能正常执行呢?_html_02

1 <body onload="func()" ryt14371="1">  2 <div id="myDIV">  3 <a href="http://www.qq.com" name="a1">http://www.qq.com</a></div>  4 <script type="text/javascript">(function embed() {  5    var oldFunc = window.func;  6   7     window.func = function() {  8         oldFunc();              // 1  9  10         // other stuff 11        alert("hello3"); 12     }; 13 })()</script></body>

View Code

也就是说,对于body的script,除了原有的onload外,还增加了一段embed的代码

这两个那个首先执行呢?

现在看貌似是embed先跑的

2)把代码中的//2这一行的注释去掉,执行时就看不到hello3对话框了,这又是为什么呢?

为啥以下网页不能正常执行呢?_javascript为啥以下网页不能正常执行呢?_html_02

1 // ==UserScript==  2 // @name        JSHE_ModifyFunction  3 // @namespace   jshe  4 // @include     http://localhost/*  5 // @version     1  6 // @grant       none  7 // ==/UserScript==  8   9 alert("hello2"); 10  11 function embed() { 12    var oldFunc = window.func; 13  14     window.func = function() { 15         oldFunc();              // 1 16  17         // other stuff 18        alert("hello3"); 19     }; 20 } 21  22 var inject = document.createElement("script"); 23  24 inject.setAttribute("type", "text/javascript"); 25 inject.appendChild(document.createTextNode("(" + embed + ")()")); 26  27 document.body.appendChild(inject); 28 //document.body.onload="embed()"; //2

View Code