2014-09-13 101 views
-1

我想阻止使用javascript加載正文,然後在body元素上寫上一些東西。使用javascript停止頁面加載

我可以做到這一點:

<html> 
<head> 
<script> 
    document.write('<!--'); 
    window.onload = function(){ 
    document.body.innerHTML = "123"; 
    } 
</script> 

</head> 
<body> 
    <script> 
     alert(1); // This won't show 
    </script> 
</body> 
</html> 

而且這個作品中,alert功能將永遠不會被調用。

但問題是,如果在頭上部的另一註釋標記,如:

<html> 
<head> 
<script> 
    document.write('<!--'); 
    window.onload = function(){ 
    document.body.innerHTML = "123"; 
    } 
</script> 

<!-- other tag --> 
</head> 
<body> 
    <script> 
     alert(1); // This will now show. 
    </script> 
</body> 
</html> 

我應該如何處理呢? body元素上的所有內容都不會加載,這一點非常重要。不只是隱藏,但沒有加載。

+2

保證的唯一方法是不寫出開頭的主體元素。例如,瀏覽器可能禁用JavaScript。 – folkol 2014-09-13 14:22:15

+3

你有什麼可能的原因呢?這是你從未回答的問題[你上次問這個問題](http://stackoverflow.com/questions/25813516/empty-body-element-before-page-renders)。 – 2014-09-13 14:25:14

回答

1

您可以嘗試

window.stop() 

Demo

注意的DOM級別0,沒有任何規範的一部分。它不被IE支持。