Javascript 中的弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16263976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:58:03  来源:igfitidea点击:

Popup window in Javascript

javascripthtmlpopup

提问by Boy Karton

In Javascript, I want to open my window.htmlfile in a popup window. But it doesn't display any text. Just a blank page.

在 Javascript 中,我想window.html在弹出窗口中打开我的文件。但它不显示任何文本。只是一个空白页。

This is index.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:

窗口.html:

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

Why doesn't it display any text?

为什么它不显示任何文本?

回答by BestDeveloper

javascript:popit(window.html);

Replace with:

用。。。来代替:

javascript:popit('window.html');

回答by Pointy

Your click handler code is syntactically incorrect:

您的点击处理程序代码在语法上不正确:

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

Always, always have your developer console open to check for JavaScript errors! (edit— actually in this case there wouldn't have been an error; window.htmlwould resolve to undefinedprobably! Still, keep the console open :-)

始终,始终打开您的开发者控制台以检查 JavaScript 错误!(编辑- 实际上在这种情况下不会有错误;window.html可能会解决undefined!不过,保持控制台打开:-)

Also note that I used an "onclick" attribute instead of "href".

另请注意,我使用了“onclick”属性而不是“href”。

回答by SeekLoad

A GOOD working code with NO crashes.

没有崩溃的良好工作代码。

Simple and what makes this code betteris that you can use it in a JavaScript file separately and have it fairing to more then one file with the same popup size even though its different pages on popups.

简单且使此代码更好的原因是,您可以单独在 JavaScript 文件中使用它,并使其适应多个具有相同弹出窗口大小的文件,即使它在弹出窗口上的页面不同。

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

HTML

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

NOTE:You can also use this as onload in body for example <body onload="JavaScript:MyPopUp('MyDirectory/Page.html');">and it will aslo work on onmouseoverand others... though I do not advise this unless you want to piss off the clients visiting your page.

注意:例如,您也可以将其用作正文中的 onload <body onload="JavaScript:MyPopUp('MyDirectory/Page.html');">,它也可以用于onmouseover其他人……尽管我不建议这样做,除非您想激怒访问您页面的客户。