Javascript not loading (why?)

3 replies
  • WEB DESIGN
  • |
Hi,

I have this on a .php page:

Code:
<script type="javascript" src="main-img-rotator.js"></script>
And the .js file contains this:

Code:
var howOften = 5; //number often in seconds to rotate
var current = 0; //start the counter at 0
var ns6 = document.getElementById&&!document.all; //detect netscape 6

// place your images, text, etc in the array elements here
var items = new Array();
    items[0]="<a href='link.htm' ><img alt='image0 (9K)' src='img-rot-01.jpg' border='0' /></a>"; //a linked image
    items[1]="<a href='link.htm'><img alt='image1 (9K)' src='img-rot-02.jpg' border='0' /></a>"; //a linked image
    items[2]="<a href='link.htm'><img alt='image2 (9K)' src='img-rot-03.jpg' border='0' /></a>"; //a linked image
    items[3]="<a href='link.htm'><img alt='image3 (9K)' src='img-rot-04.jpg' border='0' /></a>"; //a linked image
    items[4]="<a href='link.htm'><img alt='image4 (9K)' src='img-rot-05.jpg' border='0' /></a>"; //a linked image
    items[5]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-06.jpg' border='0' /></a>"; //a linked image
	items[6]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-07.jpg' border='0' /></a>"; //a linked image
	items[7]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-08.jpg' border='0' /></a>"; //a linked image
	items[8]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-09.jpg' border='0' /></a>"; //a linked image
    items[9]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-10.jpg' border='0' /></a>"; //a linked image
	items[10]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-11.jpg' border='0' /></a>"; //a linked image
	items[11]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-12.jpg' border='0' /></a>"; //a linked image
	items[12]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-13.jpg' border='0' /></a>"; //a linked image
    items[13]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-14.jpg' border='0' /></a>"; //a linked image
	items[14]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-15.jpg' border='0' /></a>"; //a linked image
	items[15]="<a href='link.htm'><img alt='image5 (18K)' src='img-rot-16.jpg' border='0' /></a>"; //a linked image
function rotater() {
    document.getElementById("placeholder").innerHTML = items[current];
    current = (current==items.length-1) ? 0 : current + 1;
    setTimeout("rotater()",howOften*1000);
}

function rotater() {
    if(document.layers) {
        document.placeholderlayer.document.write(items[current]);
        document.placeholderlayer.document.close();
    }
    if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current]
        if(document.all)
            placeholderdiv.innerHTML=items[current];

    current = (current==items.length-1) ? 0 : current + 1; //increment or reset
    setTimeout("rotater()",howOften*1000);
}
window.onload=rotater;
//-->
All image files are there in the directory, of course.
Not everything is set in the .js file (like alt text, link), but this is just a test version of an image rotator, anyway...

All files, everything is supposed to be on the root directory and it will rotate on my index.php (main) page.

Can't figure out why it's not loading. Probably a small thing I omitted.

Could it be a PHP-HTML issue? Like incompatibility within the script...

Million thanks to anyone who can help!
#javascript #loading
  • Profile picture of the author Brandon Tanner
    "detect netscape 6" ???

    I think you found a reeeeeeeeally old script. lol

    To answer your question...

    <script type="javascript" is wrong. If you're using XHTML, it should be... <script type="text/javascript"

    Or if you're using HTML5, then you don't even need the "type" attribute.

    Also, there's some other weird things in the code that don't make any sense... not to mention the entire script looks hugely inefficient. You could accomplish the same thing with a lot less code using jQuery. Google "jQuery image rotator" or "jQuery image slider"... lots of examples out there.
    Signature

    {{ DiscussionBoard.errors[8722054].message }}
  • Profile picture of the author shipwrecked
    Yes, it's an old one, but got it from someone who claims it works.

    I tried "text/javascript" first, but didn't work, so I dropped "text".

    Perhaps you are right, I should try jQuery.
    {{ DiscussionBoard.errors[8724963].message }}
    • Profile picture of the author alexstone
      You should add jquery library upper the line of your script. It`ll work!!!
      {{ DiscussionBoard.errors[8725568].message }}

Trending Topics