SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy confirm exit of page to a link in your page.

    Hello everybody, im having a little trouble.

    What i want to do is have this link on my website, [this is one of my fairly pointless things i do anyway ], which points to a passward-protected directory on my website. This part works fine.

    What im having trouble with is is i want a confirm box, which lets the page load if yes is chosen but doesnt if cancel is chosen. And i cant get this simple thing to work!

    here is my code so far:

    PHP Code:
    window.onload initAll;

    function 
    initAll() {
        
    document.getElementById("redirect").onclick initRedirect;
    }

        function 
    initRedirect() {
            if (
    confirm("Are you sure you want to do that?")) {
            
    window.location this;
                                                                }
        else {
            
    window.stop();
        }

        } 
    HTML Code:
    <li><a href="secure_plasmarox" onclick="secureplasmarox()" id="redirect">  Plasmarox's Files </a></li>

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this maybe:

    Code Javascript:
    function initRedirect() {
        if (confirm("Are you sure you want to do that?")) {
            window.location = this.href;
        } else {
            return false;
        }
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can simply return true for the link to work as expected and false if you want to cancel it.

    return confirm("Are you sure you want to do that?");

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also in that code you don't need the onclick in the anchor tag since you are assigning the onclick event in your script.


Tags for this Thread

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
  •