irkevin Posted March 21, 2009 Share Posted March 21, 2009 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 More sharing options...
jackpf Posted March 21, 2009 Share Posted March 21, 2009 What does the function do..? The function is only run when the page is loaded, so it should only run once unless your refreshing the page or something... Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790447 Share on other sites More sharing options...
irkevin Posted March 21, 2009 Author Share Posted March 21, 2009 Well, since im clicking on lets say 'HOme' , the page would refresh .. isnt it? Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790452 Share on other sites More sharing options...
jackpf Posted March 21, 2009 Share Posted March 21, 2009 Well yeah, you see, onload means when the page loads. So, each time you load the page the function will run. What is it that you're trying to achieve? Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790457 Share on other sites More sharing options...
irkevin Posted March 21, 2009 Author Share Posted March 21, 2009 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'? Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790462 Share on other sites More sharing options...
jackpf Posted March 21, 2009 Share Posted March 21, 2009 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'); } Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790466 Share on other sites More sharing options...
irkevin Posted March 21, 2009 Author Share Posted March 21, 2009 it keeps running :s Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790477 Share on other sites More sharing options...
jackpf Posted March 21, 2009 Share Posted March 21, 2009 Oooh...are you getting an error about modifying header information? If so, you'll have to use put ob_start(); at the top of your script and ob_end_flush(); at the bottom. Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790478 Share on other sites More sharing options...
irkevin Posted March 21, 2009 Author Share Posted March 21, 2009 oops. there no header errors on my website :s... can you check again plz and confirm? Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790484 Share on other sites More sharing options...
jackpf Posted March 21, 2009 Share Posted March 21, 2009 Are you sure there aren't any errors? Try putting this in the script... if(isset($_COOKIE['done'])) { echo $_COOKIE['done']; } else { echo 'cookie not set'; } Link to comment https://forums.phpfreaks.com/topic/150497-run-javascript-function-only-once/#findComment-790488 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.