views:

186

answers:

3

Apologies if this is a stupid question as Im completly new to JAvascript and web programming!

I'm currently using Dreamweaver to do some test scripts and the internal JS editor is highlighting a syntax error with my script. The only problem is that there is no indication as to what the error is or what I might need to do to fix it!

When I paste this code directly into the web page, the script seems to work without issue!

My script is as follows (saved in an external .js file):

1  // JavaScript Document
2  <script type="text/javascript">
3  
4  function coloralternatetablerows(id)
5  {
6      // If the tag exists within the document
7      if(document.getElementsByTagName)
8      {  
9          // rest of the script ommitted for clarity
10     }
11 }

The synax error is highlighted as line 7.

Can anyone help me figure this out?!

More importantly... can anyone direct me to a good resource to help me with this sort of issue in the future?

+1  A: 

You need to pass an argument to the getElementsByTagName function:

if(document.getElementsByTagName('div'))
Darin Dimitrov
Still, `if(document.getElementsByTagName)` is not a syntax error. It may be a *usage* error....
Crescent Fresh
Not if he's testing to see if the `getElementsByTagName` method is supported by the browser - it's called feature detection.
NickFitz
@NickFitz: thus the "may", judging from the line just above it: `// If the tag exists within the document`
Crescent Fresh
+3  A: 

Your Javascript code is in a seperate .js file so you don't need the <script> tag in there.

Get rid of the <script> tag completely.

In your HTML page you'll be loading in your external .js file using <script type="text/javascript" src="myscript.js"></script> but the .js file its self should have no HTML in it (like the <script> tag)

Jamie Dixon
Detting rid of the <script> Tag solved the issue! Also thanks for the snippit of information on how to call the script from the web page as well ... really useful!
TK
A: 

Do you mean:

if(document.getElementById(id) != null)

That'll check whether an element with your specified id exists.

Robert Grant