Jump to content

run javascript function only once


irkevin

Recommended Posts

Is this possible..

 

i have a function which is in the head section,, then i call it with

 

<body onload="myFunc();">

 

the problem is, i want this function to run only once.. sadly, when the user click a link, the function keeps running on and on. how to prevent this?

Link to comment
https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/
Share on other sites

function getB()
{
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{ //test for MSIE x.x;
		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion>=8 || ieversion>=7 || ieversion>=6 || ieversion>=5)
		{
			t1 = new Tween(document.getElementById('showIT').style,'height',Tween.strongEaseOut,0,300,2);
			t2 = new ColorTween(document.getElementById('showIT').style,'backgroundColor',Tween.strongEaseIn,'E8E8E8','000000',1)
			t1.start();
			t2.start();

			t1.onMotionFinished = function()
			{
				var show = document.getElementById('showText');
				show.innerHTML = "<b>MESSAGE FROM MU-ANIME STAFF</b><br /><br /><b>We've noticed that you're using Internet Explorer to surf the website. Well, Internet Explorer is not good. Believe us.<br /><br /> We recommend you using the great Mozilla Firefox to surf Mu-anime. With Mozilla you can surf peacefully and<br /> experience the website at 100%... also your life won't be a mess with IE. Trust Us.</b><br /><br /><img src='images/ff.png' /><br /><br /><a href='http://www.mozilla.com/firefox/' target='_blank' class='moz'>GET MOZILLA FIREFOX</a>    <a href='#' onclick='goOut()' class='moz'>NO I'M FINE THANKS</a><br /><br /><a href='#' onclick='goOut()' class='moz'>Close this notice</a><br /><br />";
			}
		}
	}
	else
	 document.write("n/a")
}

<body onload="getB();">

 

basically, it checks to see if the user is using IE, if so, it display a message.. is there something else to use instead of 'onload'?

 

Hmm...well, I'd personally use php.

 

You could do something like this..?

 

if(!isset($_COOKIE['done']))
{
echo '<script type="text/javascript">
//all your javascript stuff here...
window.onload = function()
{
yourFunction();
}
</script>';
$expire = time() + time();
setcookie('done', 'done', $expire, '/', '.yourdomain.co.uk');
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.