Question

I'm having problems with this code.. Basically, I'm making a website right now, and when I'm making a Confirm pop-up box when visiting the website, I make the dialogue, no problem, but when it comes to the "Ok" and "Cancel" buttons, they both allow access into the website. What I want is for the "Ok" button to proceed the user into the site, but the "Cancel" button to close the tab. How do I do this?

This is the code:

<!doctype html>
<html>
  <head>
  <script>
  var r=confirm("If you agree you are at least 18 Years of Age, press Ok. Otherwise, press Cancel.");
  if (r==true) {
    x="You pressed OK!";
  }
  else {
    x="You pressed Cancel!";
  }
  </script>
</head>
</html>
Was it helpful?

Solution

what you CAN do is empty the document content and tell the user that he/she is not allowed to view the site's content, you can't close the browser window/tab.

but if you are opening your site from another page using window.open() then open it in OK/true condition in the confirm message in that page else close the window using window.close() in the cancel/false condition

to clear out content

document.body.innerHTML = '';

http://jsfiddle.net/wzqFB/1/

OTHER TIPS

First of all the confirm works perfectly. Can check over here http://jsfiddle.net/E43gD/

Second, if you need to close the current tab using javascript, you need to use

window.close();

More over here How to close current tab in a browser window?

You can place this script at the end of body. So whenever user presses OK, you can simply execute empty function and whenever user clicks on "no", you write a function redirect him to another page for example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top