Joseph Mulako

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromJoseph Mulako
ToMe
SubjectLoading an external javascript file with document.write in another javascript file
Date8 October 2004 00:00
Dear,


Please help me to figure out what I have not got properly after reading
your  invaluable help on Incorporating JavaScript into your page.

I have an external javascript file that I would like to call when
writing the html. I am writing the html from another javascript file
using document.write(). It is giving me an error on line 3 sometimes and
sometimes seems to have load but I cannot access the functions in it.

The Javascript file that is writing the html and calling the external
one is as follows:
    var display = top.main.document;
    display.open();
    display.write('<html><head>');
    display.write('<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">\n');
    display.write('<LINK HREF="core.css" REL="stylesheet" TYPE="text/css">');
    display.write('<LINK HREF="bpage.css" REL="stylesheet" TYPE="text/css">');
    //load the functions
    display.write('<script type="text/javascript" language="javascript">\n');
    display.write('<!-'+'-\n');
    display.write('<script type="text/javascript" src="loadImages.js">');
    display.write('<\/script>\n');
    display.write('//-');
    display.write('//-'+'->\n');
    display.write('</script>\n');
  
    display.write('</head>\n');
    display.write('<body>\n');
    display.write('<body onLoad="javascript:MM_preloadImages(\'menu_home_f2.jpg\',\'menu_about_f2 .jpg\')">\n');
    // there is some other code that I think is not causing the problem.
    display.write('</body></html>\n');
    display.close();


The javascript file (loadImages.js) with the function MM_preloadImages
is as follows:

[function copyright MacroMedia, reproduced only as part of email content]
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0;i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image;d.MM_p[j++].src=a[i];}}
}

All the files and images reside in the same folder.


Anticipating your help.


Joseph Mulako.
FromMe
ToJoseph Mulako
SubjectRe: Loading an external javascript file with document.write in another javascript file
Date8 October 2004 08:39
Joseph,

I can see a few little problems with the script as it currently stands:

display.write('<script type="text/javascript" language="javascript">\n');
This is fine, but you need to close it before you open a new script tag.

display.write('<!-'+'-\n');
this is not needed. you are already running a script, so you know that the
browser understands it. As a result, there is no need to comment it out.

display.write('<script type="text/javascript" src="loadImages.js">');
This is ok, but you now have two script tags open. you sould close the
other one first

display.write('<\/script>\n');
Correct

display.write('//-');
display.write('//-'+'->\n');
Not needed - see above

display.write('</script>\n');
It will fall over here. You need to use \/ not / - most scripters make this
mistake and virtually all examples you see on the web will use use
something like '</sc'+'ript>' - however, the correct way is the way you did
the earlier tag. As a secondary point, this tag should appear before the
second opening <script tag.

display.write('</head>\n');
display.write('<body>\n');
display.write('<body onLoad="javascript:MM_preloadImages(\'menu_home_f2.jpg\',\'menu_about_f2.jpg\')">\n');
// there is some other code that I think is not causing the problem.
display.write('</body></html>\n');
display.close();
This is all fine, and will not couse problems, but strictly speaking, you
should use \/ instead of /

This is how I think it should look:
var display = top.main.document;
display.open();
display.write('<html><head>');
display.write('<meta http-equiv="Content-Type" content="text\/html; charset=windows-1252">\n');
display.write('<LINK HREF="core.css" REL="stylesheet" TYPE="text\/css">');
display.write('<LINK HREF="bpage.css" REL="stylesheet" TYPE="text\/css">');
//write your own scripts
display.write('<script type="text/javascript" language="javascript">\n');
display.write('function blah() { alert(\'write any functions you need here\'); }');
display.write('<\/script>\n');
//load the external scripts
display.write('<script type="text/javascript" src="loadImages.js">');
display.write('<\/script>\n');
display.write('<\/head>\n');
display.write('<body>\n');
display.write('<body onLoad="javascript:MM_preloadImages(\'menu_home_f2.jpg\',\'menu_about_f2.jpg\')">\n');
// there is some other code that I think is not causing the problem.
display.write('<\/body><\/html>\n');
display.close();

Hope this helps,


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromJoseph Mulako
ToMe
SubjectRe: Loading an external javascript file with document.write in another javascript file
Date11 October 2004 15:21
Thanks ever so much.
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.