Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: onUnload site exit message (exclude current domain)

  1. #1
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default onUnload site exit message (exclude current domain)

    I am trying to launch a popup window when someone exits my site (as in when they press 'home' in their browser). I do not want the popup to launch when they click on an internal link.

    I was able to get a simple script to work using 'onUnload'. The problem is that it opens a popup when the navigation menu is clicked (a link to another page on my site).

    I wanted to exclude my domain from the onUnload function. Is this possible?

    I think I want to combine this
    Code:
    <script language="javascript">
    
    /*
    Auto center window script- Eric King (http://redrival.com/eak/index.shtml)
    Permission granted to Dynamic Drive to feature script in archive
    For full source, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
    */
    
    var win = null;
    function NewWindow(mypage,myname,w,h,scroll){
    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
    settings =
    'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
    win = window.open(mypage,myname,settings)
    }
    
    </script>
    and this
    Code:
    <BODY onUnload="NewWindow('http://yahoo.com','name','400','400','yes');return false">
    with this
    Code:
    <script language="JavaScript1.2">
    <!--
    
    //Open offsite links in new window option- By Jessica Hammer
    //Heavily modified by Dynamic Drive
    //Visit http://www.dynamicdrive.com for this script
    
    //1)Enter domains to be EXCLUDED from opening in new window:
    var excludedomains=["dynamicdrive.com", "javascriptkit.com"]
    
    //2)Automatically open offsite links in new window? (1=yes, 0 will render a checkbox for manual selection)
    var auto=1
    
    var excludedomains=excludedomains.join("|")
    rexcludedomains=new RegExp(excludedomains, "i")
    
    if (!auto)
    document.write('<form name="targetmain"><input type="checkbox" name="targetnew" checked onClick="dynamiclink()">Open off-site links in new window</form>')
    
    function dynamiclink(){
    
    if (auto||(!auto&&document.targetmain.targetnew.checked)){
    for (i=0; i<=(document.links.length-1); i++) {
    if (document.links[i].hostname.search(rexcludedomains)==-1&&document.links[i].href.indexOf("http:")!=-1)
    document.links[i].target="_blank"
    }
    }
    else
    for (i=0; i<=(document.links.length-1); i++) {
    if (document.links[i].hostname.indexOf(mydomain)==-1)
    document.links[i].target=""
    }
    }
    
    if (auto)
    window.onload=dynamiclink
    
    // -->
    </script>
    Does anyone know how to combine these? If you know of a better way to launch a site exit message, please tell me (btw, this is not an ad, it is a epilogue-type message like "Thanks for visiting" or similar)

  2. #2
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default FairWell

    Actually, what I'm looking for is more like JavaScript Kit's FairWell window launcher at http://www.javascriptkit.com/script/cut65.shtml but I still would like it to ignore pages with my own domain in the url. Any ideas?

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    We had something like this recently.
    Code:
    <script type="text/javascript">
    var leaving = true;
    function checkUrl(href) {
      leaving = ((href.indexOf("://") > -1) || (href.indexOf("www.") > -1));
      // add in more things to include in the line above as you see fit
    }
    function pop() {
      if(leaving) window.open("thanks.htm");
    }
    window.onunload = pop;
    var e = documents.getElementsByTagName("a");
    for(var i=0;i<e.length;i++) e[i].onclick = "checkUrl(this.href)";
    </script>
    <a href="http://www.example.com/">Click here and you'll see a popup window</a> 
    <a href="internal.htm">Click here and you won't</a>
    It's rather early in the morning, so there are probably a couple of bugs
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't have external links on my page. I'm trying to get a popup window to appear when someone presses back or home or types in a new url in their browser. I can get onUnload to do this but I want it to exclude internal links on my site (page to page navigation).

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The above will do this.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    // add in more things to include in the line above as you see fit


    Maybe I forgot to mention I'm a complete newb . I'm really sorry but what do I need to edit? Thanks again for your help.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Quote Originally Posted by me
    leaving = ((href.indexOf("://") > -1) || (href.indexOf("www.") > -1));
    Add in || (href.indexOf("somethingElseThatDenotesAnExternalURI") > -1). The above will probably be OK, as long as all your external links use http(s):// and don't leave the browser to fill it in.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Feb 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Twey, I am in the exact same boat as snakecrosscrown. I am a dumb marketing guy who can barely read HTML but have been forced into due diligence on this one.

    So we have a web site with hundreds of pages, will your code still work or do we have to name all of the internal pages that we dont want the pop up to trigger on?

    Thanks,

    Andrew

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, it will all work, so long as the pages are on the same domain. Instead of:
    Code:
      leaving = ((href.indexOf("://") > -1) || (href.indexOf("www.") > -1));
    you might want to use
    Code:
      leaving = (((href.indexOf("://") > -1) || (href.indexOf("www.") > -1)) && (href.indexOf(window.location.host) == -1));
    This should allow you to use absolute URIs for your pages.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Feb 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey! All of our internal links are indeed on the same domain. I can't emphasize enough how dumb I am when it comes to coding, expecially JS So we are popping a usibility survey, let's say the popup url is http://www.survey.com and our site is http://www.widgets.com

    Can I just simply paste this chunk of code in between body tags?

    What exactly do I find & replace with the url examples (pop up url: www.survey.com, my site = www.widgets.com) ? (This is my biggest challenge as I literally don't know what characters are reserved words and which I am supposed to replace/edit)


    <script type="text/javascript">
    var leaving = true;
    function checkUrl(href) {
    leaving = (((href.indexOf("://") > -1) || (href.indexOf("www.") > -1)) && (href.indexOf(window.location.host) == -1));
    // add in more things to include in the line above as you see fit
    }
    function pop() {
    if(leaving) window.open("thanks.htm");
    }
    window.onunload = pop;
    var e = documents.getElementsByTagName("a");
    for(var i=0;i<e.length;i++) e[i].onclick = "checkUrl(this.href)";
    </script>
    <a href="http://www.example.com/">Click here and you'll see a popup window</a>
    <a href="internal.htm">Click here and you won't</a>


    Also, are the below lines of code optional? I am not sure I understand if they will visibly appear as clickable links on our site and I can leave them out?

    <a href="http://www.example.com/">Click here and you'll see a popup window</a>
    <a href="internal.htm">Click here and you won't</a>


    Any further insite would be greatly appreciated!

    Andrew

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
  •