Forum Moderators: open

Message Too Old, No Replies

Script working locally in Firefox but not on-line

calls variables declared in exterior js file

         

louponne

8:41 am on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello folks,

I have a drop-down menu script that, within a page, generates the content of the drop-downs by cycling through an array of variables - and those variables are in an exterior js script.

That is, within my page, I have something like this (I have simplified the code):


<script language="JavaScript" src="../data.js"></script>
<script language="javascript1.2">
<!--
for (w=0; w < 4; w++) {
document.write("<div id=submenu>");
for (j =1; j < 4; j++) {
menu_item = menuData[w][j][1];
document.write("<a href=\"" + menu_item + "\">" + menuData[w][j][0] + "</a><br>");
}
document.write("</div>\n");
}
//-->
</script>

The array menuData is declared and filled in the exterior file data.js

In IE, this works fine locally and on-line. In Firefox, the script works on my local machine, but not on-line.

Any ideas here?

Thanks!

kaled

10:19 am on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Few tips, but I doubt any is the cause of you problems.

1) Before calling document.write() call document.open()
2) Use single quotes.
3) Escape closing tags.
4) Explicitly declare all variables.
5) Remove language attrib and use type="text/javascript"

e.g.


<script type="text/javascript">
<!--
var menu_item;
document.open();
for (var w = 0; w < 4; w++) {
document.write('<div id=submenu>');
for (var j = 1; j < 4; j++) {
menu_item = menuData[w][j][1];
document.write('<a href="' + menu_item + '">' + menuData[w][j][0] + '<\/a><br>');
}
document.write('<\/div>\n');
}
//-->
</script>

louponne

11:02 am on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Many thanks, kaled, I will definitely follow your advice - but I can't see why any of these things would be why this works locally and not on-line?

kaled

4:25 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree, none of my suggestions ought to make a difference.

You should try opening a console window (under the Tools menu in Firefox) this will display javascript errors (if any).

You should also ensure that both your html and CSS code validate (but most CSS validation warnings are trivial).

The possibility also exists that a page has been cached wrongly. I always use F5 in Opera to clear server caching issues. You can then use Ctrl-F5 in Firefox.

Kaled.

louponne

5:22 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh gads I am soo embarrassed :-o

It was a very stupid error in my css.

Sorry to have bothered you folks, and thanks again, kaled, for your tips - I will implement them all to upgrade my code to a higher quality! :-)

louponne

5:41 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hm, I do have another question - is there any way to see the code that the javascript writes to the browser? If you look at the "source code" via the browser, of course you see the javascript - I mean to see the code that the script "writes"?

DrDoc

6:28 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



alert(body.innerHTML);

:)

louponne

8:05 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks DrDoc! :-)

DrDoc

8:17 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace the "alert" with whatever works best for you. If you have a lot of content, you may want to consider putting a textarea at the bottom of the page.

<textarea onclick="this.value = body.innerHTML;"></textarea>

Page refresh required inbetween each click, though.

louponne

10:23 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh, wow, yes that's much better still - that way I can copy&paste it - thanks again! :)