calling javascript function once - from the <body onload="myfunction"> tag

Discussion in 'Javascript' started by lwhitb1, Nov 22, 2005.

  1. lwhitb1

    lwhitb1 Guest

    I have been trying to load a javascript function from the body onload
    html tag, but I only want the function to load the first time the page
    is loaded: I have investigated but haven't found anything that works..
    I thought about cookies, but what if the user's disable them?


    My code:

    //I want to call the Toggle function below only on the 1st time the
    page is loaded:

    <script language="javascript">

    function Toggle(item) {

    obj=document.getElementById(item);
    visible=(obj.style.display!="none")
    key=document.getElementById("x" + item);
    if (visible) {
    obj.style.display="none";


    } else {
    obj.style.display="block";


    }

    }


    function Expand() {
    divs=document.getElementsByTagName("DIV");
    for (i=0;i<divs.length;i++) {
    divs.style.display="block";
    key=document.getElementById("x" + divs.id);
    key.innerHTML="<img src='textfolder.gif' width='175' height='25'
    hspace='0' vspace='0' border='0'>";
    }
    }

    function Collapse() {
    divs=document.getElementsByTagName("DIV");
    for (i=0;i<divs.length;i++) {
    divs.style.display="none";
    key=document.getElementById("x" + divs.id);
    key.innerHTML="<img src='Button1_01_over.gif' width='179'
    height='37' hspace='0' vspace='0' border='0'>";
    }
    }

    </script>

    // C# code I inserted above the body tag on my aspx page:

    function handler()
    {
    if(<%=!Page.IsPostBack%> )
    {
    Toggle('support');
    }

    }

    //The body tag with handler being called:

    <body onload="handler()">

    FYI: The Toggle('suport); function is not being executed. If i put the
    function in the <body onload tag.. it does run..
     
    lwhitb1, Nov 22, 2005
    #1
    1. Advertising

  2. lwhitb1

    VK Guest

    lwhitb1 wrote:
    > I have been trying to load a javascript function from the body onload
    > html tag, but I only want the function to load the first time the page
    > is loaded: I have investigated but haven't found anything that works..
    > I thought about cookies, but what if the user's disable them?


    And what if the user disabled JavaScript itself? Or it doesn't have a
    screen? Or his computer doesn't work? Or he's using text-only browser?
    Just start thinking of all these possibilities and you'll get a
    sleepless night for sure ;-)

    I would use session cookie (no expiration time) - they are allowed on
    the most strict cookie filters. Still few users may have cookie
    disabled whatsoever. This newsgroup FAQ explains how to check is
    cookies enabled:

    <http://www.jibbering.com/faq/#FAQ4_4>

    Another way would be to set the search part of the URL to some flag to
    check later:

    http://www.myserver.com/index.html?v

    and then

    if (document.location.search) {
    // she already was here
    }
     
    VK, Nov 22, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ~~~ .NET Ed ~~~
    Replies:
    5
    Views:
    1,419
    David Wainwright
    Mar 27, 2007
  2. Starry Gordon
    Replies:
    1
    Views:
    120
  3. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    614
    Martin Honnen
    Nov 4, 2004
  4. lwhitb1
    Replies:
    1
    Views:
    125
    Evertjan.
    Nov 22, 2005
  5. Replies:
    2
    Views:
    104
    Thomas 'PointedEars' Lahn
    Feb 24, 2008
Loading...

Share This Page