SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Yes And No Alert

    I'm trying to set up a form or onClick event that Confirms if the user wants to leave a web page. If they click Confirm they are taken to yahoo.com and if they click cancel they stay at the same page.

    I can't seem to get the below code to work. PLEASE Help!! Thanks everyone!

    <script type="text/javascript">
    var answer = Confirm: ("Do you want a different page?")
    if (answer)
    window.location = "http://www.yahoo.com"
    else
    alert("Nothing happened")
    </script>
    <form>
    <input type="button" value="Cheese" onclick="answer();">
    </form>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var answer = confirm("Do you want a different page?")

  3. #3
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do I apply this to a button? Right now it works on load. THANKS

  4. #4
    SitePoint Member
    Join Date
    Oct 2006
    Location
    Eindhoven, The Netherlands
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right now this code will just be processed when it's loaded in the page. Normally you would declare such functionality in a function in the <head> of your document and then call that function with for instance an onclick event from a button.

    So for instance you could use the following code:

    The code which goes between <head> and </head>
    Code:
      <script type="text/javascript">
        function reDirect() {
          var answer = confirm("Do you want a different page?");
    
          if(answer == true) {
            window.location = "http://www.yahoo.com";
          } else {
            alert("Nothing happened");
          }
        }
      </script>
    The code which goes in <body>, </body> where you want the button to be.
    Code:
      <form>
        <input type="button" value="Cheese" onclick="return reDirect();">
      </form>
    Hope this helps you along.


Bookmarks

Posting Permissions

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