473,657 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.write, script tags, and IE (execution order)

4 New Member
I'm little more than a novice when it comes to javascript, and this particular problem has been driving me mad for the past few days...

The Problem:

I have a javascript file that uses document.write to send output to the browser. This javascript file contains some html code and another script tag.

The execution order in Internet Explorer (6&7) appears to be different than that of browsers such as FireFox.

Example:

myjs.js
Expand|Select|Wrap|Line Numbers
  1. var output = '<ul><li id=\"myli\"><scr'+'ipt type=\"text/javascript\" src=\"3rdparty.js\"></scr'+'ipt></li></ul>';
  2. document.write(output);
  3.  
3rdparty.js
Expand|Select|Wrap|Line Numbers
  1. document.write('3rd party output');
FireFox Output:
<ul><li>3rd party output</li></ul>

Internet Explorer:
<ul><li></li></ul>3rd party output

Experimenting:

I don't have control over the content / output of the 3rdparty.js file, and the html code in the output var always arrives as a string (although it can be processed server-side using php if need be).

I therefore did a bit of reading up on DOM functions such as appendChild, and tried to combine it with usage of innerHTML to include the html string in a div, then execute the script tag.

myjs.js (amended)
Expand|Select|Wrap|Line Numbers
  1. function scripts(id) {
  2.     var el = document.getElementById(id).getElementsByTagName("script");
  3.     var limit = el.length;
  4.     for(var x=0; x < limit; x++) {
  5.         newScript = document.createElement("script");
  6.         newScript.type = "text/javascript";
  7.         if(el[x].text != "") {
  8.             newScript.text = el[x].text;
  9.         } else {
  10.             newScript.src = el[x].src;
  11.         }
  12.         document.getElementById(id).innerHTML = "";
  13.         document.getElementById(id).appendChild(newScript);
  14.     }
  15. }
  16. var div = '<div id=\"mydiv\"></div>';
  17. var output = '<ul><li id=\"myli\"><scr'+'ipt type=\"text/javascript\" src=\"3rdparty.js\"></scr'+'ipt></li></ul>';
  18. document.write(div);
  19. var el = document.getElementById('mydiv');
  20. el.innerHTML = output;
  21. scripts('myli');
  22.  
This method hung FireFox (it didn't seem to like my use of appendChild?) and produced the same output for IE as before.

If anyone else with a greater understanding of javascript could point me in the right direction, I'd really appreciate it.
Mar 17 '09 #1
7 8961
acoder
16,027 Recognized Expert Moderator MVP
Is there any chance you could change document.write (in the 3rd party file) to DOM statements? For example:
Expand|Select|Wrap|Line Numbers
  1. var el = document.createElement("div");
  2. txt = document.createTextNode("some output");
  3. el.appendChild(txt);
then this div could be appended to the list after page load.
Mar 18 '09 #2
sj071
4 New Member
Thanks for the reply.

Do you mean re-writing the 3rd party file itself to use DOM statements? If so, that won't be possible in all cases unfortunately (it may not always be the same javascript file).

If the html in the "myjs.js" file were to be split up so that each section was placed in a different function, and then each function executed once the other had fully loaded, would that overcome the sequencing issue?

Expand|Select|Wrap|Line Numbers
  1. function write(output) {
  2.     document.write(output);
  3. }
  4.  
  5. var block1 = '<ul><li id=\"myli\">';
  6. var block2 = '<scr'+'ipt type=\"text/javascript\" src=\"3rdparty.js\"></scr'+'ipt>';
  7. var block3 = '</li></ul>';
  8.  
  9. //begin immediately
  10. write(block1);
  11. //begin once block1 finished
  12. write(block2);
  13. //begin once block2 finished
  14. write(block3);
I realise that doesn't include any event driven callback (as I'm not quite sure how to do that), but would the theory work?
Mar 18 '09 #3
acoder
16,027 Recognized Expert Moderator MVP
Possibly, but a timeout may help:
Expand|Select|Wrap|Line Numbers
  1. setTimeout(function() { write(block2); }, 1000);
Mar 18 '09 #4
sj071
4 New Member
The timeout didn't seem to help, even when substituting the script tag for some plain text. I'm thinking that is due to the use of the deprecated document.write?

I'm therefore back to square 1 (almost). You mentioned using DOM functions earlier instead of document.write. Is there a wrapper function that can replace it, so that I could write each one of the blocks to the DOM, one by one?

Expand|Select|Wrap|Line Numbers
  1. var block1 = '<ul><li>';
  2. var block2 = '<scr'+'ipt type=\"text/javascript\" src=\"test.js\"></scr'+'ipt>';
  3. var block3 = '</li></ul>';
Mar 18 '09 #5
acoder
16,027 Recognized Expert Moderator MVP
If you can change just the document.write part, then there is a simple solution. Change document.write to:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("myli").innerHTML = "3rd party output";
Then add a function onload which adds this script to the head:
Expand|Select|Wrap|Line Numbers
  1. window.onload=function() {
  2.     var head = document.getElementsByTagName("head")[0];
  3.     var script = document.createElement("script");
  4.     script.type = "text/javascript";
  5.     script.src = "3rdparty.js";
  6.     head.appendChild(script);
  7. }
Mar 19 '09 #6
sj071
4 New Member
Thanks for helping out, I appreciate it. Sorry it's taken a while for me to reply.

Expand|Select|Wrap|Line Numbers
  1. document.getElementById("myli").innerHTML = "3rd party output"
Did you mean for the code above to be a replacement for document.write in the 3rdparty.js file in first post example? If so, then I can't do that, as I can't modify the 3rdparty.js file.

It seems like there might not be a solution, unless I've mis-understood your post above?
Apr 19 '09 #7
acoder
16,027 Recognized Expert Moderator MVP
Unfortunately, that is what I did mean. One possibility is to write the content to some element(s) and then use DOM methods to move it around, e.g. if written to a temporary holder <div id="temp"></div> and then set the li content to this div content after the page has loaded. Not optimal, but at least it would guarantee correct results.
Apr 21 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3023
by: Adam | last post by:
Hey gang, I'm trying to have a certain piece of html be written into a document depending on the browser (specifically, Netscape). Anyway, I have the following INSIDE the html (nested in a table) and wonder if this is incorrect. Can someone give me some pointers? It's been a while since I've done any javascripting and this will help so much. Thank you!! <script language="javascript"> <!-- hide javascript
6
12972
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java scripts. Being a javascript newbie and after significant testing, I suspect that the document.write fails after finding a </script> within pageVar. Does a trick exist that enables one to slightly alter pageVar whereby enabling...
12
3328
by: Radek Maciaszek | last post by:
Hi It's very interesting problem. I couldn't even find any inforamtion about it on the google. I think that the best way of explain will be this simple example: <html> <body> <script language="JavaScript" type="text/javascript" src="write.js"></script>
4
2944
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400, like DOS, uses memory-mapped display, two bytes per character (one char byte and one attribute byte). We can get the screen offset allright, and I've written a javascript which does the math to convert the offset into row/col (i.e. left, top)...
12
2843
by: Sean | last post by:
Hi, I have the following script: ----------------------------------------------------------------------------------- <script type="text/javaScript"> <!-- document.write('<div id=hello1>Hello1</div>'); document.write('<div id=hello2 style="display:none;"><script src="test.js"><\/script></div>');
136
9305
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
11
42804
by: Erwin Moller | last post by:
Hi group, I stumbled on something strange. I simplified the problem to this: A straightforward page with some JS: <span id="testid"> Hi
11
3096
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a non-trivial exercise. inserting '&lt;' and '&gt;' causes the browser to write the text to the page as literal text rather than as the intended script element. Using escape codes seemed to work (makes it standard compliant) but the text is not written to...
9
2200
by: sonnystarks | last post by:
I am taking a course in writing javascript and it (and all the books I have been reading) tell me that if I will use the document.write syntax, I will be able to "place text on the page." None of these sources tell me why I would do that instead of using ordinary HTML which is less typing. (I think I understand that it is somehow necessary if I am going to include text in a javascript function such as a calendar/clock, but no source...
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.