www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: pop up script on exit

  1. #1
    Join Date
    Nov 2002
    Posts
    68

    Talking pop up script on exit

    looking for a script that makes a page pop up after the surfer leaves my homepage not when my homepage loads but when they leave only...thanks

  2. #2
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,203
    <SCRIPT language=JavaScript>
    var exit=true;
    function goodbye()
    {
    if (exit)
    open("yourpage.htm"); <!--Place your path and file name here-->
    }
    </SCRIPT>


    Add onunload=goodbye() to the opening <Body> tag

    <BODY onunload=goodbye()>

  3. #3
    Join Date
    Nov 2002
    Posts
    68
    thanks.....

  4. #4
    Join Date
    Nov 2002
    Posts
    68

    1 other question

    is it possible to have the exit page only load if they exit at the moment it loads even if the user clicks a link to load another page the exit page still comes up.. I only want it to come up if they exit full stop, if they click on a link from my page i dont want the exit page to load....is that possible??

  5. #5
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,203
    You could possibly go:


    <script>
    <!--
    not_leaving=0
    function goodbye(){
    if(not_leaving==1){
    return
    }
    open("yourpage"); <!--Place your path and file name here-->
    }
    // -->
    </script>

    The above script would have to go in every page then include

    onclick="goodbye(not_leaving=1)"

    in all your links

    <a href="yourpage.htm" onclick="goodbye(not_leaving=1)">LINK 1</a>

    Still add onunload="goodbye()" to the opening BODY tags

  6. #6
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Originally posted by Mr J
    You could possibly go:
    J, I don't think that would work. I would pass a variable to the function like this:


    <script type="text/javascript">
    <!--
    var not_leaving=0;
    function goodbye(not_leaving){
    if(not_leaving==1){return;}
    window.open("yourpage"); //Place your path and file name here
    }
    // -->
    </script>

    <a href="yourpage.htm" onclick="goodbye(1);">LINK 1</a>


    If you have many links on the page, you may want to use JavaScript to populate their onClick functions dynamically. (E.g., a for() loop to go through all of the links on the page and set their onClick functions to goodbye(1);.)

    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  7. #7
    Join Date
    Jan 2003
    Location
    Atlanta, GA
    Posts
    571
    Older days, this would be another site I wouldn't want to visit. But now, I am equipped with Opera. Throw all the popups you want, world! I am ready for you!!

    Jona, I think that you may be confusing scope of the variable not_leaving. I don't know javascript much, but based on my programming knowledge, for the function
    function goodbye(not_leaving)
    scope of this not_leaving variable is limited to the function... this variable is different from the external variable defined as not_leaving=0;

    You may have function
    goodbye(not_leaving) {
    if(not_leaving) { return;}
    window.open("popup.html");
    }
    Note that onclick, you'll call goodbye(1)...
    But will browser execute this function twice - once onclick and next onunload? If it does, this method wont work.

  8. #8
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Originally posted by nkaisare
    Older days, this would be another site I wouldn't want to visit. But now, I am equipped with Opera. Throw all the popups you want, world! I am ready for you!!
    I agree with you. I never make use of popups unless, for example, there is a need for it. But 99% of the time, I go with something else.

    Originally posted by nkaisare
    Jona, I think that you may be confusing scope of the variable not_leaving. I don't know javascript much, but based on my programming knowledge, for the function
    function goodbye(not_leaving)
    scope of this not_leaving variable is limited to the function... this variable is different from the external variable defined as not_leaving=0;
    That is incorrect. What I have done is set not_leaving as a global variable, with a value of zero. When you click a link, it will set not_leaving to 1, thus returning the function and stopping before it goes on to opening a new window. Although, I did forget to mention the slight change in the onUnload function:


    <body onUnLoad="goodbye(0);">


    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  9. #9
    Join Date
    Jan 2003
    Location
    Atlanta, GA
    Posts
    571
    Originally posted by Jona
    <body onUnLoad="goodbye(0);">
    In which case, you do not need to pre-define it as a global variable.

    BTW, when one clicks a link, and there are onclick and onunload event handlers, wouldn't both of them be called? In that case, onclick will not cause a popup, but onunload will.

    I guess you should not pass not_leaving as a variable to the function; instead you should have
    <a onclick="not_leaving=1; goodbye()">...

  10. #10
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,203
    J, I don't think that would work
    I tried it before I posted it and it worked

  11. #11
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Well, like I said, I didn't think it'd work. I never tried editing a variable while passing it to the function. Never mind what I said, then.

    ...And I hate popups anyways.


    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  12. #12
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,203
    I don't know wether it is a standard proceedure but I have done it with more than one script and it's always worked

  13. #13
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Originally posted by Mr J
    I don't know wether it is a standard proceedure but I have done it with more than one script and it's always worked
    Have you tested it in all browsers? (Including NN4.)

    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  14. #14
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,203
    I have tried it in IE5.5 and NS7

    NN4 will not be around forever and only delays the inevitable

  15. #15
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    I know, but I was just wondering if you had tested in NN4. Netscape (the browser) is supposedly going out of "business."

    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles