简体   繁体   English

如何通过单击按钮自行关闭网页

[英]How to close the webpage on its own on clicking a button

I have a page where a <button> is kept on which onClick() event fires and redirects the new page in new tab.我有一个页面,其中保留了一个<button>onClick()事件会在该页面上触发并在新选项卡中重定向新页面。 Meanwhile I want the parent page to close itself as soon as the <button> is clicked with opening the new page in new tab.同时,我希望在单击<button>并在新选项卡中打开新页面后,父页面立即关闭。 Its basically for some security feature for the website.它基本上用于网站的某些安全功能。 How can this be done?如何才能做到这一点? Here is my <button> , let me know where can i correct myself.这是我的<button> ,让我知道我可以在哪里纠正自己。

<button formtarget="_blank" type="submit" name="submit" onClick="javascript:window.open('quiz.php?unit_id=<?php echo $fnc->encode($unit_id) ; ?>');self.close();" value="submit" class="btn btn-info" style='margin-left: 35%;margin-bottom: 10px;' ><i class="glyphicon"></i> Start Quiz</button>

you can close window by below function您可以通过以下功能关闭窗口

function close(){
 setTimeout("window.close()", 500);
}

But keep in mind that : Scripts may close only the windows that were opened by it.但请记住:脚本可能只关闭由它打开的窗口。

The window close and open new tab窗口关闭并打开新标签

<script language="javascript">

function quitWindow(cmd) 
{      
    if(window.open("your_new_tab_page.htm")){
        if (cmd=='quit')    
        {
           open(location, '_self').close();    
        }   
    }
    return false;   
}

</script>

<input  type="submit" onclick="return quitWindow('quit');" value="Close This Window and open new Tab" /> 

If you use firefox, you should about:config by writing url bar and set如果你使用 firefox,你应该写 url bar 并设置 about:config

dom.allow_scripts_to_close_windows = true

otherwise does not work firefox否则不工作火狐

I think this might work.我认为这可能会奏效。

    <script>

    function openWindow( url )
    {
            window.open(url, '_blank');
            window.focus();
            window.close();
    }

    </script>
    <button onclick="javascript:openWindow(yourHref);return false;">Submit</button>

You cant close your webpage if the user clicked the link of your website from any other place other than your website, due to security reasons.出于安全原因,如果用户从您网站以外的任何其他地方点击您网站的链接,您将无法关闭您的网页。

脚本可以关闭仅由脚本打开的站点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM