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

L

lwhitb1

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..
 
V

VK

lwhitb1 said:
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
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top