SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 via local links 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","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>

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Put you script in the head of your side with a method name. Then on the body tag, put the onload attribute and execute the name of your method. eg: <body onload="cycle()">

  3. #3
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NightStalker-DNS View Post
    Put you script in the head of your side with a method name. Then on the body tag, put the onload attribute and execute the name of your method. eg: <body onload="cycle()">
    Thank you for your reply. What you probably consider baby stuff I consider an alien language! Your suggestion that I "Put you script in the head of your site with a method name" seems simple but it creates additional questions for me:

    1: If I want to use the method name: loadmyscript exactly how do I format it when placed within my head tag?

    2: The entire script I provided in my original message is suppose to be inserted where I want the banner rotator to appear, exactly what am I putting at the insertion point and how should it look?

    If you would provide a more basic but detailed explanation I would greatly appreciate it.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •