【问题标题】:automatic open pop-up new window using Javascript [duplicate]使用Javascript自动打开弹出新窗口[重复]
【发布时间】:2013-10-18 01:12:59
【问题描述】:

使用Javascript加载页面时如何打开弹出新窗口?

我想,当一个网站加载时,它会自动打开弹出新窗口。

我使用以下:

<script type="text/javascript">
function open_on_entrance(url,name)
{ 
   window.open('http://www.google.com','google', ' menubar,resizable,dependent,status,width=300,height=200,left=10,top=10')
}
</script>
<body onload="open_on_entrance()"></body>

不工作??

【问题讨论】:

  • 你不能这样做。这是一个浏览器安全功能。某些功能(例如打开弹出窗口)必须与用户操作相关联,例如单击按钮。你可以弹出一个&lt;div&gt;,这可能是一种做你想做的事的方式。
  • 谢谢@MikeW。但我想显示新窗口。这样就没有其他办法显示了吗?
  • 如果您必须有一个新窗口,那么您必须让用户点击某些东西 - 一个丑陋的“点击此处进入”,其中包含 window.open()点击处理程序应该可以工作,但它看起来很糟糕。或者,使用&lt;iframe&gt; 弹出&lt;div&gt; 可能会满足您的需求。
  • 我测试了您的确切代码,似乎在 chrome 版本 36.0.1985.125 m 上运行良好。确保您没有启用弹出窗口阻止程序。我在我的一些代码上也使用了类似的功能,它工作正常。这是链接stackoverflow.com/q/24830249/2666522 我唯一遇到的问题是如果弹出窗口在再次重新打开之前被最小化,我会关注它。

标签: javascript popup window


【解决方案1】:

试试这个-

<script type="text/javascript">
window.onload = function() {
   window.open('http://upcominggames2015.blogspot.in/','new games release',' menubar=0, resizable=0,dependent=0,status=0,width=300,height=200,left=10,top=10')
}
</script>

将此代码粘贴到您网站的 head 部分。

【讨论】:

    【解决方案2】:

    你不能这样做。这是一个浏览器安全功能。尝试使用jQuery UI: Dialog

    http://jqueryui.com/dialog/

    例子:

        <html>
          <head>
              <meta charset="utf-8">
              <title>jQuery UI Dialog - Default functionality</title>
              <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
              <script src="//code.jquery.com/jquery-1.10.2.js"></script>
              <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
              <script>
              $(function() {
                  $( "#dialog" ).dialog();
              });
              </script>
          </head>
          <body>
            <div id="dialog" title="Basic dialog">
                <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
            </div>
          </body>
        </html>

    【讨论】:

    • 是的,我们可以。 URL 缩短器可以做到这一点。
    【解决方案3】:
    <html>
    <head>
    <script type="text/javascript">
    window.onload = function() {
       window.open('http://www.google.com','google',' menubar=0, resizable=0,dependent=0,status=0,width=300,height=200,left=10,top=10')
    }
    </script>
    <body>
    </body>
    </head>
    </html>
    

    【讨论】:

    • 感谢您的回答,但没有奏效。您还有其他想法吗?
    • 这行不通 - 这与浏览器安全有关。
    • @MikeW - 此代码已在 Google Chrome 和 IE 10 中测试,并成功运行。
    • 只有在关闭弹出窗口阻止程序时才有效。默认情况下,弹出窗口阻止程序往往是打开的,并且需要特定的用户操作才能禁用它们。 mrTom 已经表示这对他不起作用。
    • @MikeW - 这不是不可避免的吗,因为它是弹出窗口的基本约束?
    【解决方案4】:
    var newwindow;
    function poptastic(url)
    {
        newwindow=window.open(url,'name','height=400,width=200');
        if (window.focus) {newwindow.focus()}
    }
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    猜你喜欢
    • 2013-06-06
    • 2023-04-03
    • 2016-11-08
    • 2023-03-06
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    相关资源
    最近更新 更多