0

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>

Ansh
  • 269
  • 2
  • 15
  • 1
    If your script did not initiate opening the window (with something like window.open), then the script in that window is not allowed to close it. Check this question https://stackoverflow.com/questions/25937212/window-close-doesnt-work-scripts-may-close-only-the-windows-that-were-opene – escapeVelocity Feb 19 '19 at 06:29
  • Wait clicking the button opens a *new tab* and you want to close the *old tab* at the same time. Unless I'm missing something, why not just open the URL in the same tab? – VLAZ Feb 19 '19 at 06:35
  • @VLAZ Thanks for prompt reply. I appreciate. Yes, I want to close the current window on clickingthe button and opening the new window in new tab. – Ansh Feb 19 '19 at 06:44
  • @Ansh But *why* a open a new tab and close the current one? Why not redirect within the same tab? You seem to be doing the same thing effectively. Without an explanation - this seems like it's unnecessary or an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – VLAZ Feb 19 '19 at 07:33
  • @VLAZ Its an online quiz module and I dont want the user to get back on some other page while attempting the quiz. – Ansh Feb 19 '19 at 07:38
  • @Ansh so it's an XY problem then. You should be able to clear the history of the back button. In addition, you can set the server to send no-cache headers and invalidate the previously sent ones. I'm not sure how myself but I've seen this technique used where you submit a page and upon going back you get a "page is outdated" message in some form, so it's doable. It's reasonably common, as well - I've seen it in a few places. There are probably other things you can do. But your question should be "how do I prevent users from going back". At the very least closing a page means it can be reopened – VLAZ Feb 19 '19 at 07:44

4 Answers4

0

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.

Rajiv
  • 1,245
  • 14
  • 28
  • Don't use strings within `setTimeout` - pass a function. A string calls `eval` which is unnecessary. Let's leave aside security aspects, at an even more basic level it's just bad from maintainability perspective. And let's take even the most basic version of that - syntax highlight won't work in your editor of choice. There are just more and more problems up the chain from there and passing a string doesn't address anything other than making the code slightly shorter as you (at best) omit typing `function() { }` – VLAZ Feb 19 '19 at 07:38
0

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

Md. Maidul Islam
  • 564
  • 6
  • 10
  • Thanks , i tried this, but the current webpage does not closes. Can something else be done? – Ansh Feb 19 '19 at 07:17
  • @Ansh If you use firefox, you should configure about:config by writing url bar and set dom.allow_scripts_to_close_windows = true otherwise does not work firefox – Md. Maidul Islam Feb 19 '19 at 08:24
  • Thanks for this. But can you demonstrate me in above code? – Ansh Feb 19 '19 at 09:16
0

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>
  • 1
    can you explain me this above mentioned answer? I mean `window.open(url, '_blank', "shilpijain" ,"modal=no");`, I couldnot get this part. – Ansh Feb 19 '19 at 07:18
  • @Ansh window.open(URL, name, specs _(optional)_ , replace _(optional)_ ) refer - [w3s](https://www.w3schools.com/jsref/met_win_open.asp) features can also be added., window.open(url, windowName, [windowFeatures]); refer - [here](https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features) – Víñịt Vịłłă Feb 19 '19 at 09:39
0

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.

Scripts can close sites that are opened by scripts only

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103