views:

260

answers:

10

Hi!

I'm getting a JS error on displaying a page: Nothing concrete is specified but the line where it seems to be thrown. When looking into the source code of the page, I see the error is thrown inside the following script, but I can't understand why! It's only about loading images!

    <SCRIPT language=JavaScript>
<!--
function newImage(arg) {
    var rslt = new Image();
    rslt.src = arg;
    return rslt;
}
function changeImages(a, b) {
    a.src = b;
}
newImage("\/_layouts\/images\/icon1.gif");
newImage("\/_layouts\/images\/icon2.gif");
// -->
</SCRIPT>

The error I am getting is when clicking on a drop down context menu on a page, for this line:

newImage("\/_layouts\/images\/icon1.gif");

Error: The object doesn't accept this property or method Code: 0

I really don't see what could happen... Any tips on what may be happening here?
Thanks a lot!

+1  A: 

Please post the rest of the offending script.

Corporal Touchy
sorry, some of the code was gone, corrected!
Layla
+1  A: 

Write proper xml with the " around attributes.

<script type="text/javascript">
function newImage(arg) {
    var rslt = new Image();
    rslt.src = arg;
    return rslt;
}
function changeImages(a, b) {
    a.src =     b;
}
newImage("/_layouts/images/icon1.gif");
newImage("/_layouts/images/icon2.gif");
</script>
Florian Bösch
I shall check that too! thank you!
Layla
...except SCRIPT isn't an xhtml attribute, script is. And there's no language attribute on script in xhtml.
Jeff Hubbard
of course since she ommited anything that indicates weather she does SGML or XML it's a tad hard to do it precisely right
Florian Bösch
A: 

should your script block not be:

<script type="text/javascript">

?

mattlant
A: 

For starters, start your script block with

<script type="text/javascript">

Not

<script language=JavaScript>

That's probably not the root of your problem, but since we can't see your script, that's about all we can offer.

Haacked
For some reason, all the scripts in that page have 'language' instead of 'type'. What's the actual difference?
Layla
"language" is the old style; in XHTML, you use "type", and you refer to it as "text/javascript"
Rob
+3  A: 

Have you tried loading your scripts into a JS debugger such as Aptana or Firefox plugin like Firebug?

KiwiBastard
THe comments are ok, these are HTML comments, not JS. Thats an archaic way not to break old browsers.
Jakub Kotrla
True - for some reason when I read the code I read it to have the <!-- before the <script> tag
KiwiBastard
A: 

You probably need to enlist the help of a Javascript debugger. I've never figured out how to make the various debuggers for IE work, so I can't help you if you're using IE.

If you're using Firefox or you CAN use Firefox, make sure you have a Tools / Javascript Debugger command. (If you don't, reinstall it and be sure to enable that option.) Next, open up the debugger, rerun the problem page, and see what comes up.

Brendan Kidwell
+2  A: 

Why are you escaping the forward slashes. That's not necessary. The two lines should be:

newImage("/_layouts/images/icon1.gif");
newImage("/_layouts/images/icon2.gif");
Haacked
I shall check that, thanks for the idea!
Layla
A: 

How are you calling changeImages? It looks as though you are not saving a reference to the images returned by newImage. You probably want to save the results of newImage and pass that to the changeImages routine. Then changeImages should look like this:

function changeImages(a, b) {
    a.src = b.src;
}

You also may want to ensure that the images have finished loading before calling changeImages.

You've posted the routine that throws the error, without posting the error or showing us how you are calling it. If none of the answers posted fix your problem then please post some detail about how you are calling the method, which specific line the error is on, and what the error message is.

Prestaul
+2  A: 

It is hard to answer your question with the limited information provided:

  1. You are not showing the complete script
  2. You never said what the exact error message is, or even what browser is giving the error.
  3. Which line number is the error supposedly coming from?

I'd recommend using Firebug in firefox for debugging javascript if you aren't already. IE tends to give bogus line numbers.

And as others have already said, the language attribute for script tags is deprecated.

The error has been updated, the complete script is there too :-(
Layla
A: 

You firebug to debug.
http://www.mozilla.com/en-US/products/download.html?product=firefox-3.0.10&amp;os=win&amp;lang=en-US
https://addons.mozilla.org/en-US/firefox/addon/1843

JSLint is also a nice resource.
http://www.jslint.com/

Using CDATA instead of the <!-- // -->
http://www.w3schools.com/XML/xml_cdata.asp

<script type="text/javascript">
<![CDATA[
]]>
</script>
Ballsacian1