#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Berlin, Germany
    Posts
    350
    Rep Power
    14

    javascript question regarding window.location.hostname


    I want to ask my visitors if they would like to subscribe to my newsletter when:

    [list=a][*]they leave my site (put in a new URL in the browser)[*]they click a link, leading to another website (outsite my domain)[*]they close the browser window[/list=a]

    So the event-handler in JavaScript, should be onUnload().

    I am using the following code:

    Code:
    <script language="JavaScript" type="text/JavaScript">
    <!--
    	function exit_pop(){
    		if(!window.location.hostname.search(/MYDOMAINNAME/)){
    			popup_win=window.open("/newsletter.cfm","","height=300,width=300,top=0,left=0,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,directories=no");
    			popup_win.focus();
    		}else{
    			// nothing
    		}
    	}
    //-->
    </script>
    MYDOMAINNAME would be, for example, if I ran ebay.com, "ebay".

    The problem is that it doesn't work. It's suppose to check the domain name from the location and whatever I do, it doesn't work.

    I am also not sure how to trigger the exit popup when someone closes the browser since I have the check for the hostname in there - the hostname is obviously gonna be the same, just the browser is being closed.

    Lots of questions, I hope someone can help.

    Thanks!


    P.S.
    I even set a cookie, so the visitor won't be bothered again with my popup when he x'ed it out.
  2. #2
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2000
    Posts
    1,058
    Rep Power
    22
    Don't know if this will make a big difference, but try:

    self.document.location.hostname.search(/MYDOMAINNAME/)


    wdn2k
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    19
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function exit_pop(){
    if(window.location.hostname != 'www.yourdomain.co.uk'){
    popup_win=window.open("page.htm",""," height=300,width=300,top=0,left=0,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titleb ar=no,toolbar=no,directories=no");
    popup_win.focus();
    }else{
    // nothing
    }
    }
    //-->
    </script>
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Berlin, Germany
    Posts
    350
    Rep Power
    14
    I tried all the suggestions and none worked unfortunately.

    I debugged it and found out that even if someone clicks on an outgoing link, before the new page is loaded, the JavaScript still thinks that it's on the correct page, at least it output the correct page (via alert()).

    I got no clue. Very open for suggestions.
  8. #5
  9. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2000
    Posts
    1,058
    Rep Power
    22
    I've never tried to do what you're doing (I find windows that pop-up when I'm trying to leave annoying), so all I can say is go find some pages that do this and check out their js. They're out there and js is hard to hide (though not too hard to make unreasonably hard to read).

    Good luck,
    wdn2k
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Berlin, Germany
    Posts
    350
    Rep Power
    14
    I have never seen a side do what i want.

    Guess, I'll have to choose another way.

    Thanks,
    Till

    --
    Till Klampaeckel
    http://www.HTMLCenter.com
    http://forums.HTMLCenter.com
    http://www.OOCenter.com

    If my posts are helpful, please me.
  12. #7
  13. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2000
    Posts
    1,058
    Rep Power
    22
    Not trying to be sarcastic, but... are you kidding? There are tons of sites out there that pop-up another window if you try to close your browser or go to another page.


    wdn2k
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Berlin, Germany
    Posts
    350
    Rep Power
    14
    Originally posted by wdn2000
    Not trying to be sarcastic, but... are you kidding? There are tons of sites out there that pop-up another window if you try to close your browser or go to another page.


    wdn2k
    Right, but this is not your average "1 page porno TGP".

    I spent some time on the net searching for a premade script, I spent a day at least with JavaScript docs and I have not found what I need. if you know otherwise, enlighten me.

    Till

    --
    Till Klampaeckel
    http://www.HTMLCenter.com
    http://forums.HTMLCenter.com
    http://www.OOCenter.com

    If my posts are helpful, please me.
  16. #9
  17. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2000
    Posts
    1,058
    Rep Power
    22
    When I'm stuck for a solution I don't discard any possibilities until I determine they won't work for me. Good luck with your project.


    wdn2k
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    19
    The script that I posted worked for me.
    It is a simplistic script and can be easily worked around.

    You are testing from a webserver and not from your own PC aren't you?

IMN logo majestic logo threadwatch logo seochat tools logo