Results 1 to 3 of 3

Thread: Redirect Links

  1. #1
    Join Date
    Oct 2007
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Redirect Links

    Is there a script or sorts that when a link, any link, on a page is clicked that it asks the user a if they want to continue to that page. If they click OK than they will continue to the page if not they click Cancel and they stay. A popup that will allow for a custom message.

    Thanks

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>Link Confirm</title>
      <script type="text/javascript">
      function confirmLinks(){
        elements = document.getElementsByTagName("a");
        for(i = 0; i <= elements.length - 1; i++)
          elements[i].onclick = function() { return confirm("Are you sure you wish to leave this website?"); }
      }
      </script>
    </head>
    <body onload="confirmLinks()">
      <p><a href="http://google.ca">Link 1</a></p>
      <p><a href="http://php.net">Link 2</a></p>
    </body>
    </html>

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It works, but XHTML isn't a good idea for the vast majority of pages, and certainly not required here. More importantly, you have exposed two undeclared global variables:

    examples

    and:

    i

    Add to that reliance upon getElementsByTagName when it is not required (limits the number of browsers the script will work on).

    Your code does work and demonstrates cleverness, but please consider this version, which doesn't suffer from any of the issues I mentioned, and that works just as well:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <title>Link Confirm</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <script type="text/javascript">
      function confirmLinks(){
        for(var l = document.links, i = 0; i <= l.length - 1; i++)
          l[i].onclick = function() { return confirm("Are you sure you wish to leave this website?"); }
      }
      </script>
    </head>
    <body onload="confirmLinks()">
      <p><a href="http://google.ca">Link 1</a></p>
      <p><a href="http://php.net">Link 2</a></p>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •