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

    Evertjan. Guest

    lwhitb1 wrote on 23 nov 2005 in comp.lang.javascript:

    > 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?



    What does "only once" mean?

    Different users from the same machine?

    Same user on different machines?

    Same user on different IP and same laptop?

    Me seems you should define your quest.

    What about having the user log in on your site,
    and using a database and serverside script get to your goal.


    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
     
    Evertjan., 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,360
    David Wainwright
    Mar 27, 2007
  2. Starry Gordon
    Replies:
    1
    Views:
    112
  3. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    559
    Martin Honnen
    Nov 4, 2004
  4. lwhitb1
    Replies:
    1
    Views:
    243
  5. Replies:
    2
    Views:
    99
    Thomas 'PointedEars' Lahn
    Feb 24, 2008
Loading...

Share This Page