Forum Moderators: open

Message Too Old, No Replies

Using document.write() to load an external javascript file

         

gerard

8:14 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



I don't know if the subject is descriptive enough. Here's an explanation of what I'm trying to do.

If I write a javascript file include line such as "<script src='/myjsfile.js'></script>" using document.writeln() will the browser actually go and fetch the file?

Sort of a dynamic way to include script files.

tedster

8:30 pm on Jul 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it should -- I assume you are writing this line in the head section, in association with some other conditional statements.

I do this in conjunction with a browser sniffer on one site and it works fine there. Be sure to include the type="text/javascript" attribute in your <src> tag.

ricfink

2:19 am on Jul 17, 2003 (gmt 0)

10+ Year Member



Browser displays and/or executes that which is written using document.write ( or writeln) same as hard-coded HTML. (But javascript has to be enabled!)
Yes, it will load the external js file.

Another way to acheive (and there are others, too) the same result is to hard-code the script tag change the src attribute using javascript. The browser should dynamically load the js file and apply to document.

Xuefer

5:03 am on Jul 17, 2003 (gmt 0)

10+ Year Member



it seems that opera6.x don't support wnd.document.write("<script>document.write...</script>");

and some browser make document written <script src=".."> to be "defer=true"

GeorgeGG

6:17 am on Jul 17, 2003 (gmt 0)

10+ Year Member



it seems that opera6.x don't support wnd.document.write
("<script>document.write...</script>");

I have Opera 6.01 and it works with:
<script TYPE="text/javascript">
document.write("<script type=\"text/JavaScript\" src=\"http://www.domain.com/file.js\"></"+"script>");
</script>

g1smd

6:38 am on Jul 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



</"+"script>");

You might also be able to escape that tag as:

<\/script>");

Xuefer

7:26 am on Jul 17, 2003 (gmt 0)

10+ Year Member



to GeorgeGG:
<script src="..">
is not the same as <script>

gerard

4:16 pm on Jul 17, 2003 (gmt 0)

10+ Year Member



Thanks everyone. It seems to be working. I had plenty of unterminated string messages, but the "<\/" helped. After doing that, I remembered I did something similar years ago using the "</" + "script>" thing. Yes, age has finally hit.

I have it working in a sample page. I will apply it to the application I have.

Thanks again. Much appreciated. :)

gerard

8:13 pm on Jul 17, 2003 (gmt 0)

10+ Year Member



A little more news.

I was using the document.write() to write the entire document contents to the page. Sounds silly, but there was a reason. It still didn't work properly no matter what sort of method I followed. What I had working earlier was a test, but the actual document still ****ed when something in the body tries to reference a function or object I had in one of those included javascript files.

The solution was to write the head and body of the document with different document.write() calls and separate them with a closing </script> tag.

Kinda like:

<script>
document.write("The entire head");
</script>
<script>
document.write("Stuff in the body");
</script>

So it works, just have to separate them out. I noticed this about IE a while back. It's a kind of flush it seems. NS7 and Opera seemed fairly ok, but had a few niggles, but they should work correctly now.

drbrain

9:35 pm on Jul 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found this gem in mozilla:

data:text/html,<script>document.write(document.location);</script>

Make sure you only paste that into your urlbar if you are prepared for a crash.

Xuefer

2:35 am on Jul 18, 2003 (gmt 0)

10+ Year Member



hrm...
dynapi used document.write() to load js, it's ok

however, when u open a window by js, and document.open(); document.write(...); document.close();
it's a bit different from document.write when loading(parsing) page