2013-04-28 116 views
1

在Javascript中,我想在一個彈出窗口中打開我的window.html文件。但它不顯示任何文字。只是一個空白頁面。使用Javascript的彈出窗口

這是index.html的:

<!DOCTYPE html> 
<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="style.css" /> 

    <script language="javascript"> 
    var newwindow; 

    function popit(url){ 
     newwindow = window.open(
      url, '', "status=yes, height=500; width=500; resizeable=0"); 
    } 
    </script> 
</head> 
<body> 
    <a href="javascript:popit(window.html);">CLICK ME!</a> 
</body> 
</html> 

window.html:

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
    <p>SAMPLE TEXT</p> 
</body> 
</html> 

爲什麼它不顯示任何文本?

+0

很多瀏覽器阻止彈出窗口... – JAAAY 2017-03-13 21:58:01

回答

2
javascript:popit(window.html); 

替換爲:

javascript:popit('window.html'); 
+1

哦,我愚蠢。非常感謝! – 2013-04-28 14:44:14

0

你的點擊處理程序代碼是語法不正確:

<a href="#" onclick="popit('window.html');">CLICK ME!</a> 

永遠,永遠有你的開發者控制檯,打開檢查JavaScript錯誤! (編輯 —其實在這種情況下,就不會出現錯誤;!window.html將解析爲undefined可能仍然,保持控制檯打開:-)

另外請注意,我用了一個「點擊」屬性,而不是「HREF」。

0

一個良好的工作代碼沒有崩潰。

簡單是什麼使這個代碼更好是,你可以用它在JavaScript文件分開,並把它整流罩更然後一個文件具有相同尺寸彈出即使在彈出窗口的不同頁面。

的Javascript

// Popup window code 
function MyPopUp(url) { 
    popupWindow = window.open(
     url,'popUpWindow','height=454,width=580,left=0,top=200,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes') 
} 

HTML

<a href="JavaScript:MyPopUp('MyDirectory/Page.html');" title="My PopUp For You">My PopUp</a> 

注:您也可以爲onload事件在身體用這個例子<body onload="JavaScript:MyPopUp('MyDirectory/Page.html');">,它會ASLO上onmouseover等工作。 ..雖然我不建議這樣做,除非你想要剝奪訪問你的網頁的客戶。