Javascript 自动弹出窗口(无需单击按钮)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9303386/
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-26 06:13:13  来源:igfitidea点击:

Javascript auto popup (without clicking a button)

phpjavascripthtmlajax

提问by Ian Taylor

I was just wondering if it is possible to display a javascript message (either popup or something nice) which is triggered by something which isn't a mouse click, so for example:

我只是想知道是否有可能显示由不是鼠标点击的东西触发的 javascript 消息(弹出窗口或其他好的东西),例如:

<?php if(a == b) {
    Then auto pop up something
}

I have had a look around and cant seem to find anything, also how do people make like stylistic popups like.. i guess maybe CSS but really fancy ones which cant be done with CSS any ideas?

我环顾四周,似乎找不到任何东西,还有人们如何制作像样式弹出窗口这样的..我想也许是 CSS 但真正花哨的那些不能用 CSS 完成的任何想法?

Thanks a bunch :)

谢谢一堆:)

回答by Gabe

Just add a script block to open an alert or what ever you need to open

只需添加一个脚本块即可打开警报或您需要打开的任何内容

<?php if(a == b) { ?>
    <script>
          alert('hello');
          // or
          window.open("http://wwwstackoverflow.com");
          // or any type of script you want to run

    </script>
<?php } ?>

回答by Anton Khamets

For anyone coming here these days: window.open()called like that will be blocked by any modern browser.

对于这些天来这里的任何人:这样的window.open()调用将被任何现代浏览器阻止。

If you're okay with including a third-party script, you can do this instead:

如果您可以包含第三方脚本,则可以改为执行以下操作:

<?php if(a == b) { ?>
  <script>
    Popup("<div>Hello</div>").show('up')
  </script>
<?php } ?>