Java Script Confirm Box

Discussion in 'Web Design and Development' started by janitorC7, Dec 4, 2007.

  1. janitorC7 macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA
    #1
    Hey guys, another question, same project.

    I open a Confirm box, and if its an OK, I want a page to open.

    But, everytime I do this, with a pop-up blocker turned on it stops the page from opening, how do I get around this.

    Also, how do I stop an onunload from being true when a site visitor clicks on a link?

    Thanks
    JC7
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    If you could get around a pop-up blocker, then it wouldn't be working correctly. Depending on the browser though, if the user is initiating the pop-up by clicking on a link the blocker won't block it.

    As for the unload question, you'd have to have a script attached to the links that'll decide whether or not to go somewhere. I've seen some scripts for this, but don't have them at hand, but just do some searches for that. Though it isn't the greatest of solutions and has accessibility problems. Canceling the unload is rather hard.
     
  3. janitorC7 thread starter macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA
    #3
    To the first part.

    The event is user triggered. It opens a new window when they hit 'ok', they have an option of clicking cancel. As this is user triggered, why would it be catching it?

    and for the second part. Is there a way to set an unload variable to false in a link, or do I need to find another way to do this.

    I have never really had to deal with JavaScript much before, as none of my clients have ever really wanted it, and I;ve consitered it more of an annoyance most of my life.

    Thanks again for your help guys, it reminds me why I come here every time I need help

    This in in the HEAD
    Code:
    <script type="text/javascript">
    <!--
    function confirmation() {var answer = confirm("Confirm Text")if (answer){ window.open('http://www.*****.com/**.html') }
    and in the BODY
    Code:
    <body onunload="confirmation()" value="Confirm Text">
    
     
  4. janitorC7 thread starter macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA
    #4
    Here I found a site that is similar to what I would like to do, not the same but similar. I want mine to open in a new page instead of the same page (I;m not sure how he did that actually)

    Link

    any ideas guys
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Well that sites doesn't actually link to other pages so it isn't a ton of help. I worked some things out and I think I have something to help.

    HTML:
    function doit(url)
    {
      x = confirm ("Really?");
      if(x) window.open(url);
    }
    ...<body>...
    <p><a onclick="doit(this.href); return false;"
    href="http://www.google.com/">Google</a></p>
    ...
    This solution is nice because if the user's browser doesn't support JavaScript (or it's turned off) the link will still work as a normal link. You'll have to add the onclick attribute to all of the links you want it applied to, but you can probably add it dynamically with JavaScript if you really wanted to. Didn't end up needing to use the onunload or onbeforeunload events for this to work either.

    My small amount of testing using Firefox, this code didn't block the new window from opening up. You'll have to check and see how other browsers handle it. I think it should be fine though. I think this does what you're looking for. If not let me know.
     
  6. janitorC7 thread starter macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA
    #6
    Thank You very much.

    But from reading your code, I;m not sure if this is quite what i;m looking for. I want the dialogue to come up on page unload, IE when them X out of the window.

    (I know that this is a bad idea as a webdeveloper, but its what the client insists on)

    Thanks again for your help...

    JC7
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    Yes, the code I provided only applies to when a link with the onclick attribute set is clicked. It doesn't work with closing the browser window. From what I read at http://www.quirksmode.org/js/events_compinfo.html the onunload cannot be canceled. My testing (with Firefox) shows once the onunload or onbeforeunload is initiated the window.open function cannot be employed. This limits your options.

    My suggestion is to provide a link on the page that says "Close this window" and attach the code I provided before to do the action you want. Just make sure it's easy to see so that users may be more likely to click it rather than the window control.
     
  8. janitorC7 thread starter macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA
    #8
    I guess that that is my only option, thanks.
     
  9. janitorC7 thread starter macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA
    #9
    okay, I have another idea. can I make it so that when a person wants to close the page, it overlays a div? like in the example I posted earlier?

    JC7
     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    Can you elaborate? From what I saw of that site it just hides one div and reveals another, which is simple to do. Just describe the steps you are thinking about and I'll provide some more detail of a solution.
     
  11. janitorC7 thread starter macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA
    #11
    okay
    I'll list the steps I see

    1. User closes the page
    2. a dialoge asks weather they would like to display another page.
    3. if they say yes, it reveals another div of the original page
     
  12. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #12
    I found some code at http://chiragrdarji.wordpress.com/2...-child-window-to-parent-window-in-javascript/ that looks to have the code that should help with this. It uses a window.opener.document to talk to the parent window. So you should be able to do something like window.opener.document.getElementById("anId"); and change the styling of the divs to hide one and reveal another.

    I don't have time right now to write it and try it, but you might be able to figure it out with help from the page above. It does look possible to do, which is good.
     
  13. janitorC7 thread starter macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA
    #13
    Yes, This looks like it is what I want, I will keep you guys posted.
     
  14. janitorC7 thread starter macrumors 6502a

    janitorC7

    Joined:
    Feb 10, 2006
    Location:
    North Hollywood, CA

Share This Page