# Question on using javascript

**URL:** https://boards.straightdope.com/t/question-on-using-javascript/628575
**Category:** Factual Questions
**Created:** [July 18, 2012, 12:34am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575 "2012-07-18T00:34:34Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)
#### Post date: [July 18, 2012, 12:34am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/1 "2012-07-18T00:34:34Z")

</div>

Here is a javascript I wrote and works fine as an HTML file

> [@](#):
>
> \<!DOCTYPE html\>  
> \<html\>  
> \<head\>  
> \<title\>JavaScript Practice\</title\>  
> \</head\>  
> \<body bgcolor=“#ffffff”\>  
> \<img src=“Chrysanthemum.jpg” height=200 length=250 id=“rotator”\>  
> \<script type=“text/javascript”\>  
> var rotpix = new Array();  
> rotpix[0]=‘Chrysanthemum.jpg’;  
> rotpix[1]=‘Jellyfish.jpg’;  
> rotpix[2]=‘Desert.jpg’;  
> var a;  
> var i=0;  
> var t=setInterval(“displaypix()”, 5000);  
> var pic=document.getElementById(“rotator”);
> 
> function displaypix() {  
> a=1;  
> fadeout();  
> var setdelay=setTimeout(“rotatepic()”, 1000);   
> }
> 
> function rotatepic() {  
> i=i+1;  
> if (i==rotpix.length) {i=0;}  
> pic.src=rotpix\*;  
> clearInterval(fadeouttime);  
> fadein();  
> }
> 
> function fadein() {fadeintime=setInterval(“fadeindummy()”, 50);}  
> function fadeindummy() {  
> pic.style.opacity=a;   
> a=a+0.05;  
> if (a\>1) {clearInterval(fadeintime); a=1;}  
> }
> 
> function fadeout() {fadeouttime=setInterval(“fadeoutdummy()”, 50);}  
> function fadeoutdummy() {  
> a=a-0.05;  
> pic.style.opacity=a;  
> if (a\<0) {a=0;}  
> }   
> \</script\>  
> \</body\>

Because I’m using it on multiple pages, I really want to make it into an external .js file. I moved  
\<img src=“Chrysanthemum.jpg” height=200 length=250 id=“rotator”\>  
into my HTML file and changed the rotation timer from a line to a function  
function startrotation () {t=setInterval(“displaypix()”, 5000);}  
so it can be called from the HTML file.  
\<input type=“button” onclick=“startrotation()” value=“Start Slideshow”\>  
So I run it and nothing. I checked and the  
\<script type=“text/javascript” src=“picturerotator.js”\>\</script\>  
is in the HEAD section so the script should be loading. My thought is that as “rotator” is initialized in the HTML document, that when the javascript has to use it, it has no clue what to do. Any ideas?

---

<div class="post-metadata">

### Author: ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)
#### Post date: [July 18, 2012, 12:46am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/2 "2012-07-18T00:46:02Z")

</div>

First thing to do is to make sure the javascript really _is_ being loaded. What browser are you using? Do you have any add-ons like web developer that will show you the loaded javascript files? Or, alternatively, when you click the button are you getting any errors in the javascript error console?

---

<div class="post-metadata">

### Author: ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)
#### Post date: [July 18, 2012, 2:08am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/3 "2012-07-18T02:08:17Z")

</div>

> [@Athena](#):
>
> First thing to do is to make sure the javascript really _is_ being loaded. What browser are you using? Do you have any add-ons like web developer that will show you the loaded javascript files? Or, alternatively, when you click the button are you getting any errors in the javascript error console?

IE8  
No indicator that it is being loaded. Is there a good add on for that?

---

<div class="post-metadata">

### Author: ![Sparklo](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Sparklo](https://boards.straightdope.com/u/Sparklo)
#### Post date: [July 18, 2012, 2:08am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/4 "2012-07-18T02:08:31Z")

</div>

When the script gets loaded in the \<head\> tag, anything that’s not a function will get executed immediately. So this line:

var pic=document.getElementById(“rotator”);

will try to grab the element with the id “rotator”. But since the document hasn’t been loaded yet, that element doesn’t exist. Then, when your functions get called, pic is undefined. I’d expect some errors to show up in the console.

As a quick fix, try putting the \<script\> tag at the bottom of the HTML file (just before the closing \</body\> tag) instead of loading it in \<head\>. That way, the document should be loaded enough to work. Another option would be to change your functions to grab the element when needed rather than when the script is read.

---

<div class="post-metadata">

### Author: ![tellyworth](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tellyworth](https://boards.straightdope.com/u/tellyworth)
#### Post date: [July 18, 2012, 2:11am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/5 "2012-07-18T02:11:53Z")

</div>

Firefox, Chrome and Safari all have excellent debuggers and developer tools. Use one of those for development. Trying to write Javascript without one is just a waste of time.

---

<div class="post-metadata">

### Author: ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)
#### Post date: [July 18, 2012, 2:18am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/6 "2012-07-18T02:18:45Z")

</div>

> [@Sparklo](#):
>
> When the script gets loaded in the \<head\> tag, anything that’s not a function will get executed immediately. So this line:
> 
> var pic=document.getElementById(“rotator”);
> 
> will try to grab the element with the id “rotator”. But since the document hasn’t been loaded yet, that element doesn’t exist. Then, when your functions get called, pic is undefined. I’d expect some errors to show up in the console.
> 
> As a quick fix, try putting the \<script\> tag at the bottom of the HTML file (just before the closing \</body\> tag) instead of loading it in \<head\>. That way, the document should be loaded enough to work. Another option would be to change your functions to grab the element when needed rather than when the script is read.

Or, if the logic works right for your needs, put that code into a function and arrange for it to be called via the window.onload event.

---

<div class="post-metadata">

### Author: ![John\_H\_1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@John\_H\_1](https://boards.straightdope.com/u/John_H_1)
#### Post date: [July 18, 2012, 2:22am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/7 "2012-07-18T02:22:20Z")

</div>

As mentioned the script tag must be placed after your image in the document like this or you’ll have to adjust the code.

```auto

<img src="1.png" height=200 length=250 id="rotator">
<script type="text/javascript" src="picturerotator.js"></script>

```

My personal recommendation for working with html/css/js would be firefox + firebug (extension).

---

<div class="post-metadata">

### Author: ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)
#### Post date: [July 18, 2012, 2:26am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/8 "2012-07-18T02:26:35Z")

</div>

> [@Saint\_Cad](#):
>
> IE8  
> No indicator that it is being loaded. Is there a good add on for that?

You should be able to use the developer tools that come with IE8 - does an option called “Developer Tools” show up under the Tools menu? I don’t think it’s an add-on but I could be wrong. Alternatively, you can pull it up with the F12 key.

Once you have them up, there’s a tab called “Script.” Click that. On the tool bar that appears, there’s a button called “Start Debugging.” To the right of that is a drop-down. If you click the down arrow on the drop-down, it will list all the loaded javascript files. Does yours show up? If it doesn’t, it’s not loading it correctly.

But also, what **tellyworth** says is right. Nowadays, there are some debugging tools built-in to IE8, but overall, I much prefer Firefox and Chrome. The Web Developer add-on for Firefox (and I think also for Chrome…) is a must-have if you’re doing this kind of stuff very often.

And everyone else who is telling you about what’s loaded when are also right - you have to load the stuff in the right place, or it won’t work.

---

<div class="post-metadata">

### Author: ![tellyworth](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tellyworth](https://boards.straightdope.com/u/tellyworth)
#### Post date: [July 18, 2012, 2:40am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/9 "2012-07-18T02:40:58Z")

</div>

IE8’s developer tools are probably fine too, I just have no experience with them (wasn’t even sure it had any).

---

<div class="post-metadata">

### Author: ![Civil\_Guy](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Civil\_Guy](https://boards.straightdope.com/u/Civil_Guy)
#### Post date: [July 18, 2012, 2:42am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/10 "2012-07-18T02:42:51Z")

</div>

I’m a javascript newb myself, and there are parts of the OP script that I don’t understand much. In the line

var t=setInterval(“displaypix()”, 5000);

Does that actually work the way it should? Should the function displaypix() be quoted as shown, or will that be interpreted as calling setInterval(var, var) with a string argument and an integer argument?

Does function displaypix() need to be defined before it’s first called?

---

<div class="post-metadata">

### Author: ![MobiusStripes](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@MobiusStripes](https://boards.straightdope.com/u/MobiusStripes)
#### Post date: [July 18, 2012, 2:49am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/11 "2012-07-18T02:49:25Z")

</div>

> [@Civil\_Guy](#):
>
> I’m a javascript newb myself, and there are parts of the OP script that I don’t understand much. In the line
> 
> var t=setInterval(“displaypix()”, 5000);
> 
> Does that actually work the way it should? Should the function displaypix() be quoted as shown, or will that be interpreted as calling setInterval(var, var) with a string argument and an integer argument?
> 
> Does function displaypix() need to be defined before it’s first called?

Oddly though it may seem, yes it’s suppose to be in quotes. The function displaypix() won’t need to be defined until the setInterval() function is called.

---

<div class="post-metadata">

### Author: ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)
#### Post date: [July 18, 2012, 5:09am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/12 "2012-07-18T05:09:21Z")

</div>

> [@Sparklo](#):
>
> When the script gets loaded in the \<head\> tag, anything that’s not a function will get executed immediately. So this line:
> 
> var pic=document.getElementById(“rotator”);
> 
> will try to grab the element with the id “rotator”. But since the document hasn’t been loaded yet, that element doesn’t exist. Then, when your functions get called, pic is undefined. I’d expect some errors to show up in the console.
> 
> As a quick fix, try putting the \<script\> tag at the bottom of the HTML file (just before the closing \</body\> tag) instead of loading it in \<head\>. That way, the document should be loaded enough to work. Another option would be to change your functions to grab the element when needed rather than when the script is read.

Great tip. I put it right after my \<img src …\> line and the pictures rotate 😃 .  
Only problem now is the fadein and fadeout functions are not working right even though they are being called.

---

<div class="post-metadata">

### Author: ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)
#### Post date: [July 18, 2012, 5:44am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/13 "2012-07-18T05:44:45Z")

</div>

> [@Saint\_Cad](#):
>
> Only problem now is the fadein and fadeout functions are not working right even though they are being called.

More info. Using IE9

The script as a self-contained HTML files fades in and out unless in compatability mode but when used as an external file in IE9 it does not fade in or out. 😕  
Firefox and Chrome fade in and out perfectly.  
So apparently the way I’m setting the alpha doesn’t work with IE, at least consistantly. Getting late so more research in the morning.

---

<div class="post-metadata">

### Author: ![tellyworth](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tellyworth](https://boards.straightdope.com/u/tellyworth)
#### Post date: [July 18, 2012, 7:08am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/14 "2012-07-18T07:08:58Z")

</div>

Use jQuery.

---

<div class="post-metadata">

### Author: ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)
#### Post date: [July 18, 2012, 9:20am UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/15 "2012-07-18T09:20:25Z")

</div>

> [@tellyworth](#):
>
> Use jQuery.

This advice, while good, is completely irrelevant to solving the OP’s problem.

---

<div class="post-metadata">

### Author: ![Kinthalis](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/kinthalis/32/16084_2.png) [@Kinthalis](https://boards.straightdope.com/u/Kinthalis)
#### Post date: [July 18, 2012, 2:09pm UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/16 "2012-07-18T14:09:57Z")

</div>

The correct way to set this up is to tie it to a browser event. Specifically one that gets called right after the DOM becomes available.

This has already been suggested, but I wanted to also recommend using an API to work with javascript.

The abstraction of the various browsers and browser versions will save your sanity.

With jquery you would call your code from within the following:

$(document).ready(function(){  
// your code here  
});

This would trigger your code the moment the DOM is fully accessible to it.

---

<div class="post-metadata">

### Author: ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)
#### Post date: [July 18, 2012, 3:38pm UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/17 "2012-07-18T15:38:52Z")

</div>

The code execution is not the problem. The problem is that IE apparent has unique ways it likes to handle alpha=\>opacity and that has been changed again for IE9. It’s just a matter of tracking down the right function.

And for the jquery advice, I have heard from people in the field that the way to go in modern times (ie 2012) is CMS.

---

<div class="post-metadata">

### Author: ![FlightlessBird](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@FlightlessBird](https://boards.straightdope.com/u/FlightlessBird)
#### Post date: [July 18, 2012, 4:31pm UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/18 "2012-07-18T16:31:01Z")

</div>

google search terms: html opacity ie9. Here are those results:  
[link](https://www.google.com/search?q=html+opacity+ie9)

This one looks promising:  
[First Link](http://blogs.msdn.com/b/ie/archive/2010/08/17/ie9-opacity-and-alpha.aspx)

Hope it helps!

---

<div class="post-metadata">

### Author: ![Kinthalis](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/kinthalis/32/16084_2.png) [@Kinthalis](https://boards.straightdope.com/u/Kinthalis)
#### Post date: [July 18, 2012, 5:35pm UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/19 "2012-07-18T17:35:05Z")

</div>

> [@Saint\_Cad](#):
>
> The code execution is not the problem. The problem is that IE apparent has unique ways it likes to handle alpha=\>opacity and that has been changed again for IE9. It’s just a matter of tracking down the right function.
> 
> And for the jquery advice, I have heard from people in the field that the way to go in modern times (ie 2012) is CMS.

Again, why using an API with javascript could help. This time it was a change in the way a particular browser version of a particular browser brand handles a particular instruction. Next time it could be 3 different browser versions from 2 different browsers having issues with 5 different pieces of code.

Other people have done the heavy lifting of abstracting that nonsense. You don’t want to waste time re-inventing the wheel.

Also, CMS? Is that supposed to be content management? That’s not really a javascript solution. Unless you meant MVC, as in model, view, control which is another way of handling complex javascript - many js API’s are available which handle MVC quite well.

---

<div class="post-metadata">

### Author: ![Blakeyrat](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@Blakeyrat](https://boards.straightdope.com/u/Blakeyrat)
#### Post date: [July 18, 2012, 6:37pm UTC](https://boards.straightdope.com/t/question-on-using-javascript/628575/20 "2012-07-18T18:37:20Z")

</div>

> [@tellyworth](#):
>
> Firefox, Chrome and Safari all have excellent debuggers and developer tools. Use one of those for development. Trying to write Javascript without one is just a waste of time.

As does IE8 and IE9. (And earlier versions of IE back to 5, but it was a separate download.)

Don’t let the anti-IE snobs get to you, hehe.

[Next page](https://boards.straightdope.com/t/question-on-using-javascript/628575.md?page=2)
