如何只打开一个窗口弹出窗口并将其限制为只有一个窗口

时间:2015-04-20 10:44:12

标签: javascript html popup popupwindow

我想在点击一个URL后只打开一个弹出窗口。 但如果我点击相同的URL,则会打开另一个弹出窗口。所以现在有两个弹出窗口。

任何人都可以帮助解决这个问题,因为我想将它限制在一个窗口。 代码: -

<script type="text/javascript">
var isOpen = "false";
var newwindow=null;
function call() {
    var link =document.getElementById('link').getAttribute('href');
    if (isOpen == "false") {
        isOpen = "true"; 
        window.open(link,'_blank','width=950,height=650,scrollbars=yes,resizable=yes');
    } else {
        newwindow.location.href = url;
        newwindow.focus();
    }

}
</script>

<body>
    <a id="link" href="welcome.htm?hello=1" onclick="call()">Link 1</a>
    <br>
    <a href="welcome.htm?hello=1">Link 2</a>
    <br />
    <a href="welcome.htm?hello=asd">Link 3</a>
    <br />


</body>

4 个答案:

答案 0 :(得分:1)

给出窗口名称,如:

window.open(link,'testName','width=950,height=650,scrollbars=yes,resizable=yes');

它将在同一窗口中打开链接。

<script >
var isOpen = "false";
var newwindow=null;
function call() {
    var link =document.getElementById('link').getAttribute('href');
    if (isOpen == "false") {
        isOpen = "true"; 
        window.open(link,'testName','width=950,height=650,scrollbars=yes,resizable=yes');
    } else {
        newwindow.location.href = url;
        newwindow.focus();
    }

}
</script>

希望它有所帮助。 Refer this.

答案 1 :(得分:0)

显示弹出窗口的函数/代码应进行一些更改以标记弹出窗口已经显示。

作为示例:以下示例更改了链接href,因此不会再次调用该函数:

<html>
    <head>
        <script>
            function myfunction () {
                document.getElementById("someid").setAttribute("href", "javascript:console.log('No more alert!')");
                alert("Will be shown only once!");
            }
        </script>
    </head>
    <body>
        <a id="someid" href="javascript:myfunction()">Click me!</a>
    </body>
</html>

答案 2 :(得分:0)

这个适合我。

var win;

$(document).on('click','#button', function(event) {
    url = 'https://example.com/';

    if (!win) { 
        win = window.open(url, '_blank');

    }else{
        win.focus(); 
    }   
}); 

答案 3 :(得分:0)

var newWindow = null;
goToUrl(url)
goToUrl = (url) => {
    if (!!!newWindow) {
        newWindow = window.open(url);
    } else {
        newWindow.focus()
        newWindow.location = url;
    }
}
相关问题