Javascript 如何打开一个 url 链接作为弹出屏幕

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

How to open a url link as a popup screen

javascriptjqueryhtml

提问by Santosh

I have an url, lets say www.myurl.com/mywebpageand when I type this url in browser it opens my page and everything works fine.

我有一个 url,可以说www.myurl.com/mywebpage,当我在浏览器中输入这个 url 时,它会打开我的页面,一切正常。

Now my requirement is that when I type this url in browser,the webpage should open like a pop up. As there is no button click or such event, I cannot write window.openjavascript function to open this as pop up.

现在我的要求是,当我在浏览器中输入这个 url 时,网页应该像弹出窗口一样打开。由于没有按钮单击或此类事件,我无法编写window.openjavascript 函数来打开它作为弹出窗口。

Is there any other way(jquery) or how we can use window.open function to show my page as popup when the url is hit in the browser.

有没有其他方法(jquery)或我们如何使用 window.open 函数在浏览器中点击 url 时将我的页面显示为弹出窗口。

Thanks in advance.

提前致谢。

回答by Santosh

just using window.open(with out any function) in html solved my problem.

仅在 html 中使用 window.open(没有任何功能)就解决了我的问题。

in page1.html i wrote the following script

在 page1.html 我写了以下脚本

<script>
window.open("http://google.com", "myWindow", 'width=800,height=600');
window.close();
</script>

When i type page1.html in browser address,it opens google page as a popup.

当我在浏览器地址中输入 page1.html 时,它会以弹出窗口的形式打开谷歌页面。

Thanks everyone for your efforts in trying to help me.

感谢大家为帮助我所做的努力。

回答by Drakes

No, there is no such function without user interaction

不,没有用户交互就没有这样的功能



You could create a dialog()widget with jQuery, however.

但是,您可以dialog()使用 jQuery创建一个小部件。

See: https://blog.udemy.com/jquery-popup-window/

见:https: //blog.udemy.com/jquery-popup-window/

Demo: http://jsbin.com/yusenu/2/

演示:http: //jsbin.com/yusenu/2/

回答by sms247

try this code: (popupex.html will replace with your page url)

试试这个代码:(popupex.html 将替换为您的页面 url)

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>click demo</title>

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>


  <a href="popupex.html" onclick="return popitup('popupex.html')"
        >Link to popup</a>
<script>
function popitup(url) {
        newwindow=window.open(url,'name','height=200,width=150');
        if (window.focus) {newwindow.focus()}
        return false;
    }
</script>

</body>
</html>