Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2013, 08:05 PM   PM User | #1
amazing2061
New to the CF scene

 
Join Date: Mar 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
amazing2061 is an unknown quantity at this point
Initialize/Load Javascript on page load from local links

The code below works fine, I think its short enough that showing it here should not offend or violate any rules. Here's the story: The code is a banner rotator script and as I said earlier it works fine. I have it installed on the home page of a mobile site. The problem is that when I visit other pages of the mobile site and then return back to the home page the banners don't rotate any more. They only start again if I refresh/reload the page manually with the browser. I guess I need to know how to make the script load every time the home page is accessed from a local site link to initialize it again. TIA

[CODE]<script type="text/javascript">
var imgs1 = new Array("http://www.nunucarservice.com/m/data1/images/nunuad.jpg","http://www.nunucarservice.com/m/data1/images/nunuad2.jpg");
var lnks1 = new Array("www.nunucarservice.com/m/help.html","http://www.nunucarservice.com/m/help.html");
var alt1 = new Array();
var currentAd1 = 0;
var imgCt1 = 2;
function cycle1() {
if (currentAd1 == imgCt1) {
currentAd1 = 0;
}
var banner1 = document.getElementById('adBanner1');
var link1 = document.getElementById('adLink1');
banner1.src=imgs1[currentAd1]
banner1.alt=alt1[currentAd1]
document.getElementById('adLink1').href=lnks1[currentAd1]
currentAd1++;
}
window.setInterval("cycle1()",3000);
</script>
<a id="adLink1" target="_top"> <img id="adBanner1" style="border: 0;" style="border: 0;" src="http://www.nunucarservice.com/m/data1/images/nunuad.jpg" src="http://www.nunucarservice.com/m/data1/images/nunuad.jpg" alt="" border="0" width="100%"></a></div>[CODE]
amazing2061 is offline   Reply With Quote
Old 03-08-2013, 07:33 AM   PM User | #2
sbhmf
New Coder

 
Join Date: Jan 2013
Location: Sunnyvale, CA
Posts: 40
Thanks: 3
Thanked 1 Time in 1 Post
sbhmf is an unknown quantity at this point
I wonder if adding an onfocus event to the document that will call the setTimeout function will get it going again...?
sbhmf is offline   Reply With Quote
Old 03-08-2013, 08:24 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Try moving the JavaScript to just before the </body> tag where most JavaScript belongs.

Moving the script will fix your problem because with the script at the top it tries to run before the HTML has loaded when it is already cached by the browser.

You should also fix the setInterval call - it expects a function not a string in the first parameter.

window.setInterval(cycle1,3000);
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-08-2013, 05:12 PM   PM User | #4
sbhmf
New Coder

 
Join Date: Jan 2013
Location: Sunnyvale, CA
Posts: 40
Thanks: 3
Thanked 1 Time in 1 Post
sbhmf is an unknown quantity at this point
Although you are correct that the object itself must be entered for the parameter and not a string, I expect that the setInterval function's parameters are already correct, since it works fine when initially loaded.

Seems to me that the script is not executing when the window displays the document from its history's cache, accessed via the browser's back button, and I wonder therefore if triggering setInterval via the document's onfocus event might relaunch script execution.
sbhmf is offline   Reply With Quote
Old 03-08-2013, 08:23 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by sbhmf View Post
Although you are correct that the object itself must be entered for the parameter and not a string, I expect that the setInterval function's parameters are already correct, since it works fine when initially loaded.
No you are forcing it to eval() the string in the first parameter. What you have will work it is just not as efficient as if you were to pass the parameter correctly.

Quote:
Originally Posted by sbhmf View Post
Seems to me that the script is not executing when the window displays the document from its history's cache,
Yes it is but it is running first before the HTML has loaded and therefore crashes because the HTML it is trying to update doesn't exist yet.

MOVE THE SCRIPT TO THE BOTTOM OF THE PAGE AND IT WILL THEN WORK.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Tags
banner rotator, intialize, javascript load

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:25 AM.


Advertisement
Log in to turn off these ads.