Results 1 to 6 of 6

Thread: Javascript Confirmation Dialog - Please Help

  1. #1
    Join Date
    Aug 2007
    Location
    MO USA
    Posts
    106
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Javascript Confirmation Dialog - Please Help

    I have a page with various logos of companies (vendors page) - when the user clicks on a logo, the corresponding company's website opens in a separate window. I want to have a javascript confirmation box before this. Once the user clicks on a logo, a box should appear with the message "You are about to leave the website, click OK to continue" - if he clicks OK, the corresponding website should be opened in a separate window. If he clicks Cancel, he should remain where he is.

    How do i go about doing this. Please Help.

    Thanks.

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Code:
    if(confirm('You are about to leave the website, click OK to continue')){
        window.open(...);
    }
    If you need more details, I do too. What's your current implementation, to start with?
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. The Following User Says Thank You to Jesdisciple For This Useful Post:

    me_myself (10-27-2008)

  4. #3
    Join Date
    Aug 2007
    Location
    MO USA
    Posts
    106
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default

    Jesdisciple, thanks for the post.

    I created a .js file with 10 different functions:
    Code:
    function vendorlnk1()
    {
    if(confirm("You are about to leave the website. Click OK to continue"))
    window.open ("http://www.company1.com/");
    }
    
    function vendorlnk2()
    {
    if(confirm("You are about to leave the website. Click OK to continue"))
    window.open ("http://www.company2.com/");
    }
    Then in the html image map, i called the function as follows:

    Code:
    <img src="images/vendors.jpg" width="516" height="606" usemap="#Map" />
    <map name="Map" id="Map">
    <area shape="rect" coords="25,497,125,466" href="#" onclick="vendorslnk1();return false;" />
    <area shape="rect" coords="15,235,133,387" href="#" onclick="vendorslnk2();return false;" />
    </map>
    The above seems to work in Firefox 3, but throws error in IE 7. Can you please check what i am doing wrong. Also please let me know if there is a better way of doing this.

    Thanks a lot for the help.
    Last edited by me_myself; 10-27-2008 at 09:19 PM.

  5. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Doubt this'll work, but try to backspace these:

    Code:
    function vendorlnk1()
    {
    if(confirm("You are about to leave the website. Click OK to continue")) window.open ("http://www.company1.com/");
    }
    
    function vendorlnk2()
    {
    if(confirm("You are about to leave the website. Click OK to continue")) window.open ("http://www.company2.com/");
    }
    or do this:

    Code:
    <img src="images/vendors.jpg" width="516" height="606" usemap="#Map" />
    <map name="Map" id="Map">
    <area shape="rect" coords="25,497,125,466" href="#" onclick="vendorslnk1();return false;" ></area>
    <area shape="rect" coords="15,235,133,387" href="#" onclick="vendorslnk2();return false;" ></area>
    </map>
    -magicyte

  6. The Following User Says Thank You to magicyte For This Useful Post:

    me_myself (10-28-2008)

  7. #5
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Now that I think about the situation more, you would be more accurate to say that the user is opening a popup (which some people find annoying) rather than leaving the website. After all, they will still have your website open but not in front.

    EDIT: Sorry, I forgot about the IE error. What is the error's information?

    This is a more compact, maintainable, and accessible way to do the same thing:
    Code:
    window.onload = function()
    {
        var vendorLinks = document.getElementsByName('vendorlnk');
        for(var i = 0; i < vendorLinks.length; i++)
        {
            vendorLinks[i].onclick = vendorLink;
        }
        function vendorLink()
        {
            if(confirm("You are about to leave the website. Click OK to continue"))
                window.open(this.href);
            return false;
        }
    };
    HTML Code:
    <img src="images/vendors.jpg" width="516" height="606" usemap="#Map" />
    <map name="Map" id="Map">
    <area shape="rect" coords="25,497,125,466" href="http://www.company1.com/" name="vendorlnk" />
    <area shape="rect" coords="15,235,133,387" href="http://www.company2.com/" name="vendorlnk" />
    </map>
    Last edited by Jesdisciple; 10-28-2008 at 12:19 AM.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  8. The Following User Says Thank You to Jesdisciple For This Useful Post:

    me_myself (10-28-2008)

  9. #6
    Join Date
    Aug 2007
    Location
    MO USA
    Posts
    106
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Thumbs up

    Jesdisciple, thanks for the script. Works great - no error.

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
  •