2014-01-28 24 views
0

test2.html使用Javascript - 的onload功能和setTimeout的不正常工作(更新)

<!DOCTYPE html> 
<html> 
<head> 
<title>test</title> 
<style> 
form { 
    width: 330px; 
    margin: 20px; 
    background-color: pink; 
    Padding: 20px; 
} 

input { 
    text-align: right; 
} 
</style> 

<script type="text/javascript"> 
    var pw; 
    function create_form(_name, _action, _method, _target) { 

     var instance = document.createElement("form"); 
     instance.name = _name; 
     instance.action = _action; 
     instance.method = _method; 
     instance.target = _target; 

     return instance; 
    } 

    function create_input_to_form(_form, _type, _name, _value) { 
     var form_instance = _form; 
     var input_instance = document.createElement("input"); 

     input_instance.type = _type; 
     input_instance.name = _name; 
     input_instance.value = _value; 

     form_instance.insertBefore(input_instance, null); 

     return form_instance; 
    } 

    function insert_form_to_html(_form) { 
     document.body.insertBefore(_form, null); 
    } 

    function init() { 

     var instance = create_form(
       "nuForm", 
       "Test3.html", 
       "post", "postWindow"); 
     insert_form_to_html(instance); 
     pw = window.open("", "postWindow", "width=300, height=400"); 
     instance.submit(); 

     if (pw == null) { 
      alert("error;"); 
     } 
     else { 
      pw.document.onload = function() { 
       alert("wow"); 
      } 
      document.write("1"); 
      pw.onload = function() { 
       alert("wow2"); 
      } 
      document.write("2"); 
      setTimeout(function(){ 
       alert("wow3"); 
       alert(pw.document.title); 
      }, 3000); 
      document.write("3"); 
     } 

    } 
</script> 
</head> 

<body onLoad="init();"> 

</body> 
</html> 

test3.html

<!DOCTYPE html> 
<html> 
<head> 
<title>Insert title here</title> 
</head> 
<body>popup bodys 
</body> 
</html> 

警報( 「WOW」);,警報( 「wow2」) ;,alert(pw.document.title);不起作用。 文件位於同一文件夾中,創建彈出窗口時不會顯示錯誤和警告。

我不明白爲什麼pw不是局部變量,但是隻能在Init()函數中使用pw變量,並在超時中分解。

i的谷歌瀏覽器


有固定test2.html代碼進行測試。

<!DOCTYPE html> 
<html> 
<head> 
<title>test</title> 
<style> 
form { 
    width: 330px; 
    margin: 20px; 
    background-color: pink; 
    Padding: 20px; 
} 

input { 
    text-align: right; 
} 
</style> 

<script type="text/javascript"> 





    var pw; 
    function create_form(_name, _action, _method, _target) { 

     var instance = document.createElement("form"); 
     instance.name = _name; 
     instance.action = _action; 
     instance.method = _method; 
     instance.target = _target; 

     return instance; 
    } 

    function create_input_to_form(_form, _type, _name, _value) { 
     var form_instance = _form; 
     var input_instance = document.createElement("input"); 

     input_instance.type = _type; 
     input_instance.name = _name; 
     input_instance.value = _value; 

     form_instance.insertBefore(input_instance, null); 

     return form_instance; 
    } 

    function insert_form_to_html(_form) { 
     document.body.insertBefore(_form, null); 
    } 

    function init() { 
     var instance = create_form("nuForm", "Test3.html", "post", "postWindow"); 
     insert_form_to_html(instance); 
     pw = window.open("", "postWindow", "width=300, height=400"); 
     instance.submit(); 

     if (pw == null) { 
      alert("error;"); 
     } else { 


      pw.onload = function() { 
       alert("wow2"); 
       console.log("4"); 
      } 
      console.log("2"); 
      console.log(pw); 
      setTimeout(function() { 
       if (pw==null) { 
        alert("pw is null"); 
       } 
       else { 
        //console.log(document.domain); 
        console.log(pw); 
        //alert(pw.title); 
       } 

      }, 3000); 
      console.log("3"); 
     } 

    } 
</script> 
</head> 

<body onLoad="init();"> 

</body> 
</html> 

第一個問題是「alert(」wow2「);」和「console.log(」4「);」代碼不會在彈出窗口完成加載時執行。

第二個問題是「console.log(pw);」到setTimeout()輸出空值,但另一個「console.log(pw)」輸出右邊的彈出窗口。爲什麼pw變量消失,儘管pw不是局部變量?

有谷歌瀏覽器控制檯時執行此文件

2 Test2.html:69 
'window.webkitStorageInfo' is deprecated. Please use 'navigator.webkitTemporaryStorage' or 'navigator.webkitPersistentStorage' instead. VM56:423 
Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…} 
Test2.html:70 
3 Test2.html:82 
Window {} 
+0

定義「不工作」。你期望發生什麼?究竟發生了什麼?當你在調試器中執行代碼時,它與你期望的有什麼不同? JavaScript控制檯中是否有錯誤? – David

+0

當你使用'document.write(「1」);'你丟失(確實是)錯誤的文檔內容。使用'console.log()'來代替調試值。 – DontVoteMeDown

回答

2

當你的腳本談到這個部分...

 pw.document.onload = function() { 
      alert("wow"); 
     } 
     document.write("1"); 
     pw.onload = function() { 
      alert("wow2"); 
     } 
     document.write("2"); 
     setTimeout(function(){ 
      alert("wow3"); 
      alert(pw.document.title); 
     }, 3000); 
     document.write("3"); 

...事件甚至可以被設置,但你失去了你的文檔內容使用document.write()。所以一切都會丟失。因此,您可以看到alert("error;");,但沒有其他alert()您的代碼。您應該使用console.log()來調試這些值。只要改變:

document.write("1"); 

到...

console.log("1"); 

,或者如果你真的想打印的價值的文件...

document.body.innerHTML = "1"; 

等出現次數。

1

可以使init方法改變象下面這樣:

if (pw == null) { 
     alert("error;"); 
    } 
    else { 

     pw.onload = function() { 
      alert("wow2"); 
     } 
     console.log("2"); 
     setTimeout(function(){ 
      alert("wow3"); 
      alert(pw.title); 
     }, 3000); 
     console.log("3"); 
    } 

更換pw.document.onload到pw.onload。現在它正在工作

你可以在這個小提琴找到。 http://jsfiddle.net/QU7nP/