简体   繁体   中英

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. Meanwhile I want the parent page to close itself as soon as the <button> is clicked with opening the new page in new tab. 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 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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