[英]Popup window in Javascript
在Javascript中,我想在彈出窗口中打開window.html
文件。 但是它不顯示任何文本。 只是一個空白頁。
這是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>
為什么不顯示任何文字?
javascript:popit(window.html);
用。。。來代替:
javascript:popit('window.html');
您的點擊處理程序代碼在語法上不正確:
<a href="#" onclick="popit('window.html');">CLICK ME!</a>
始終,始終打開您的開發人員控制台以檢查JavaScript錯誤! ( 編輯 —實際上,在這種情況下不會出現錯誤; window.html
可能會解析為undefined
!仍然保持控制台打開:-)
另請注意,我使用的是“ onclick”屬性而不是“ href”。
良好的工作代碼,沒有崩潰。
簡單且使此代碼更出色的是,您可以在一個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>
注意:您也可以將其用作<body onload="JavaScript:MyPopUp('MyDirectory/Page.html');">
,例如<body onload="JavaScript:MyPopUp('MyDirectory/Page.html');">
,它也可以在onmouseover
和其他程序上工作...盡管我不建議這樣做除非您想惹惱訪問您網頁的客戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.