Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    90
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Unhappy OnUnload Refresh

    Hello,

    I have an onunload event, but i dont want it to start during refresh. Is there a way to stop that.

    -Steven

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    You might be able to run it when one of your navigation items is clicked, but you will be able to get better responses if you post your code.

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    90
    Thanks
    5
    Thanked 3 Times in 3 Posts
    all i want is an onunload event to occur when user leaves page, but i have a form and when user clicks submit, the onunload event also occurs which i dont want.

    sorry for the inconvenience.
    Last edited by BigRusky; 08-06-2008 at 01:09 AM.

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Have you tried to create a global variable, and set it either 1/0. If the user submits, then set that variable to 0 and in your onunload function, have something along this lines:
    Code:
    var flag=1;
    onunload=function(){
    if(flag) // Message when the user leaves
    }
    Hope that makes sense. If nothing works, up the code so we could have something to play with.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    New Coder
    Join Date
    Aug 2008
    Posts
    90
    Thanks
    5
    Thanked 3 Times in 3 Posts
    how would u submit the flag var

    my code:

    PHP Code:
    <?
    include "auto.php";
    $page = new ArmyPage("onunload=\"steven();\"");

    if (isset(
    $_POST['submit_chat']) && $_POST['submit_chat'] == "Submit") {
    if (
    $_POST['msg'] != "") {
    $query mysql_query("INSERT INTO chat (name, msg, aid) VALUES ('".$_SESSION['name']."', '".$_POST['msg']."', '".$_GET['a']."')") or die(mysql_error());
    } else {
    //Do nothing
    }}else{
    }

    //rank check:
    if ($_SESSION['arank'] == "Leader") {
    $rank "leader";
    } elseif (
    $_SESSION['arank'] == 1) {
    $rank 1;
    } else {
    $rank "all";
    }

    $page->PrintHeader();
    ?>
    <script language="javascript" type="text/javascript">
    new Ajax.PeriodicalUpdater('content', 'chatdiv.php?a=<? echo $_GET['a'?>', {
      frequency: 2, decay: 1
    });
    </script>
    <div id="chat">
    <div class="header">
    Welcome to chat!
    </div>
    <div id="content">
    <?
    $q 
    mysql_query("SELECT * FROM chat WHERE aid = '".$_GET['a']."' ORDER BY `id` DESC") or die(mysql_error());
    while (
    $r mysql_fetch_array($q)) {
    ?>
    <span class="name"><? echo $r['name'?>:</span>
    <span class="msg">
    <? echo $r['msg'?>
    </span><br />
    <?
    }
    ?>
    </div>
    <a name="bottom"></a>
    </div>
    <div id="submit">
    <form method="post" action="achat.php?a=<? echo $_GET['a'?>">
    <input type="text" size="35" name="msg" />
    <input name="submit_chat" type="submit" value="Submit" />
    </form>
    </div>
    <?
    $page
    ->PrintFooter();
    ?>

    Note: the unonload event is attached to the body tag through classes
    Last edited by BigRusky; 08-06-2008 at 05:56 AM.

  • #6
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    And the steven function?
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #7
    New Coder
    Join Date
    Aug 2008
    Posts
    90
    Thanks
    5
    Thanked 3 Times in 3 Posts
    o sry that was a test

    that is the onunload function its just not defined.
    Last edited by BigRusky; 08-06-2008 at 05:37 AM.

  • #8
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Here's a basic example of what I believe would work:
    Code:
    <script type="text/javascript">
    var flag=1;
    window.onload=function(){
    document.forms[0].onsubmit=function(){flag=0;}}
    window.onunload=function(){
    if(flag) alert('I know, you are leaving');}
    </script>
    <a href="http://www.google.com">test [Google]</a><br>
    <a href="http://www.codingforums.com">test [CodingForums]</a>
    <form action="http://www.codingforums.com">
    <input type="submit" value="Submit">
    </form>
    It does'nt alert any message when you submit the form. Only when submitting the form.
    Hopefully, this should work at your end.
    Last edited by rangana; 08-06-2008 at 06:17 AM. Reason: Explain more.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    BigRusky (08-06-2008)

  • #9
    New Coder
    Join Date
    Aug 2008
    Posts
    90
    Thanks
    5
    Thanked 3 Times in 3 Posts
    thanks ur code works but my class system is all messed up and is ruining it so i have to look at that.

  • #10
    New Coder
    Join Date
    Aug 2008
    Posts
    90
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Hello again,

    I have got the onunload event to finally work! But, ....

    Is there some way to stop the event from happening when the page is refreshed? Like, is there a way to find out what page the person came from, when going to this one?

    -Steven
    Last edited by BigRusky; 08-23-2008 at 05:19 AM.


  •  

    Posting Permissions

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