Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Script works in Firefox, not in IE

Reply
Thread Tools

Script works in Firefox, not in IE

 
 
cjl
Guest
Posts: n/a
 
      06-08-2005
Hey all:

Just getting my feet wet with javascript. The following script works
fine in Firefox, but won't run in IE. In fact, I've included this
script as an external file in the <head> of my html file, and the
remainder of the page doesn't load at all in IE. If I remove the
<script>, the page loads in IE.

var images = new Array();
var firstImage = 2;
var lastImage = 27;
var timeout_state = null;
var counter = 0;
var running = 0;

for (counter=firstImage;counter<=lastImage;counter++)
{
images[counter] = new Image();
images[counter].src = counter + ".jpg";
}

counter = firstImage;

function switchImage()
{
if(running == 0)
{
running = 1;
animate();
}
else
{
running = 0;
clearTimeout(timeout_state);
timeout_state = null;
}
}

function animate(element)
{
if (counter >= lastImage) {counter = firstImage;}
document.getElementById('right').src = images[counter].src;
counter++;
timeout_state = setTimeout("animate()", 50);
}

Any help?

-CJL

 
Reply With Quote
 
 
 
 
ASM
Guest
Posts: n/a
 
      06-08-2005
cjl wrote:
> Hey all:
>
> Just getting my feet wet with javascript. The following script works
> fine in Firefox, but won't run in IE. In fact, I've included this
> script as an external file in the <head> of my html file, and the
> remainder of the page doesn't load at all in IE. If I remove the
> <script>, the page loads in IE.



> function animate(element)
> {
> if (counter >= lastImage) {counter = firstImage;}
> document.getElementById('right').src = images[counter].src;


try :
document.getElementById('right').src = eval(images[counter]+'.src');

try also putting a var right=''; at top of JS
or prefer :
document.getElementById(element).src = images[counter].src;
and
timeout_state = setTimeout("animate("+element+")", 50);

IE doesn't like uknonwn objects and in your JS
the page object 'right' is used but not yet seen

> counter++;
> timeout_state = setTimeout("animate()", 50);
> }


and overall don't run animate() before complete loading of the page
-> preferable to put a JS for that just before </html>

and let some time to IE it catches your 26 images


--
Stephane Moriaux et son [moins] vieux Mac
 
Reply With Quote
 
 
 
 
cjl
Guest
Posts: n/a
 
      06-08-2005
Hey all:

OK, the problem was with my html, not my javascript.

I had:
<script src="pre.js" >

When I needed
<script src="pre.js" ></script>

I wasn't providing the closing tag, which IE 6 didn't like, but Firefox
ignored.

-CJL

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      06-08-2005
On 08/06/2005 14:40, ASM wrote:

> cjl wrote:
>
>> Just getting my feet wet with javascript. The following script works
>> fine in Firefox, but won't run in IE.


I cannot see anything in what you've posted that will not work in IE
5.x+. Please post a URL to an example - it's much easier to diagnose.

Some alternative code is included at the end of this post (though it is
untested).

[snip]

> try :
> document.getElementById('right').src = eval(images[counter]+'.src');


Randomly throwing eval about will *NEVER* solve a problem.

> try also putting a var right=''; at top of JS


What do you think that will do?

> or prefer :
> document.getElementById(element).src = images[counter].src;
> and
> timeout_state = setTimeout("animate("+element+")", 50);


That seems sensible, but it won't fix the OP's code.

[snip]

Mike


function Animation(name, images, rate) {
var current = 0,
element = document.images[name],
timer;

function animate() {
element.src = images[current];
current = (current + 1) % images.length;
timer = setTimeout(animate, rate);
}

if(!element || ('function' != typeof setTimeout)
|| ('function' != typeof clearTimeout))
{
return null;
}

animate.toString = function() {
return 'Animation[' + name + ']();';
};
Animation[name] = animate;

this.preload = function() {
if('function' == typeof Image) {
for(var i = 0, n = images.length, t; i < n; ++i) {
t = new Image();
t.src = images[i];
}
}
};
this.reset = function() {
this.stop();
current = 0;
};
this.start = function() {
if(!timer) {
timer = setTimeout(animate, rate);
}
};
this.stop = function() {
if(timer) {
clearTimeout(timer);
timer = null;
}
};
}


A usage example:

var myImages = [],
i = 2,
n = 29,
myAnimation;

/* Define the array of images in the animation.
* It could be performed in any way, but this
* seems to suit your situation the best.
*/
while(i <= n) {
myImages[myImages.length] = i + '.jpg';
}

/* Create an Animation object. */
myAnimation = new Animation('right', myImages, 50);

/* If it was created properly, */
if(myAnimation) {
/* attempt to preload the frames */
myAnimation.preload();
/* and start the animation when the document
* loads.
*/
window.onload = function() {
myAnimation.start();
};
}

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getMethod() works and works not Alexander Burger Java 25 11-29-2010 06:33 PM
In a template, Eval () does not works in fields ofcontrols, it works abargaddon ASP .Net Web Controls 1 02-04-2008 09:16 PM
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
[py2exe.i18n] English works, German works, but not French. What do I miss? F. GEIGER Python 3 08-06-2004 10:01 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM



Advertisments