Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Closing a confirm() box

    I am still learning JavaScript and have created a JavaScript confirm() box in which if you press OK you are sent to a website. In this instance BBC News for want of something better. But if you press on cancel is it possible to close the window?

    Code:
    	<head>
    		<meta charset="utf-8"/>
    		<title>JavaScript Confirm</title>
    		<script>
    		  if(confirm("If you don't agree with my rules then click on the Cancel button"))
    			{
    				window.location.href="http://www.bbc.co.uk/news/uk/";
             		}
    		   else
                            {
    				self.close();	
    			}
    		</script>
    	</head>

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Looks like it should work. Are you having issues with it?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by Frump View Post
    I am still learning JavaScript and have created a JavaScript confirm() box in which if you press OK you are sent to a website. In this instance BBC News for want of something better. But if you press on cancel is it possible to close the window?
    If you mean can you bypass the warning given by Internet Explorer, the answer is no. But otherwise it works - what exactly is your problem?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hello there
    If a user didn't want to go to the website i have specified in the code, i would like the user to press on "Cancel" and the window to be closed, instead it takes me to a blank screen?

    Just thought i'd mention that i am running a Linux OS called CrunchBang Linux and using Firefox as my default browser.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    The problem to me seems to be the various security measures implemented by the different browsers. On my versions:
    Chrome closes the window
    IE asks if you want to close the window
    FF ignores the close() completely

    I think as a general rule the only window you can be sure that you can close is one that you have opened programatically.

  • #6
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Pity really as my browser of choice is Firefox. But perhaps there's a way around this by using setTimeout() and setInterval()?

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Nope. Using those you're just delaying the inevitable.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    JavaScript is not allowed to close a window it didn't open (except in IE which asks you to confirm it first).

    Of course if your visitor selects the checkbox in the confirm dialog to turn off JavaScript then it isn't going to process either the OK or Cancel options.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •