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 - var output = '<ul><li id=\"myli\"><scr'+'ipt type=\"text/javascript\" src=\"3rdparty.js\"></scr'+'ipt></li></ul>';
-
document.write(output);
-
3rdparty.js - 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) - function scripts(id) {
-
var el = document.getElementById(id).getElementsByTagName("script");
-
var limit = el.length;
-
for(var x=0; x < limit; x++) {
-
newScript = document.createElement("script");
-
newScript.type = "text/javascript";
-
if(el[x].text != "") {
-
newScript.text = el[x].text;
-
} else {
-
newScript.src = el[x].src;
-
}
-
document.getElementById(id).innerHTML = "";
-
document.getElementById(id).appendChild(newScript);
-
}
-
}
-
var div = '<div id=\"mydiv\"></div>';
-
var output = '<ul><li id=\"myli\"><scr'+'ipt type=\"text/javascript\" src=\"3rdparty.js\"></scr'+'ipt></li></ul>';
-
document.write(div);
-
var el = document.getElementById('mydiv');
-
el.innerHTML = output;
-
scripts('myli');
-
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.
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: - var el = document.createElement("div");
-
txt = document.createTextNode("some output");
-
el.appendChild(txt);
then this div could be appended to the list after page load.
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? - function write(output) {
-
document.write(output);
-
}
-
-
var block1 = '<ul><li id=\"myli\">';
-
var block2 = '<scr'+'ipt type=\"text/javascript\" src=\"3rdparty.js\"></scr'+'ipt>';
-
var block3 = '</li></ul>';
-
-
//begin immediately
-
write(block1);
-
//begin once block1 finished
-
write(block2);
-
//begin once block2 finished
-
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?
acoder 16,027
Recognized Expert Moderator MVP
Possibly, but a timeout may help: - setTimeout(function() { write(block2); }, 1000);
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? - var block1 = '<ul><li>';
-
var block2 = '<scr'+'ipt type=\"text/javascript\" src=\"test.js\"></scr'+'ipt>';
-
var block3 = '</li></ul>';
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: - document.getElementById("myli").innerHTML = "3rd party output";
Then add a function onload which adds this script to the head: - window.onload=function() {
-
var head = document.getElementsByTagName("head")[0];
-
var script = document.createElement("script");
-
script.type = "text/javascript";
-
script.src = "3rdparty.js";
-
head.appendChild(script);
-
}
Thanks for helping out, I appreciate it. Sorry it's taken a while for me to reply. - 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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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>
|
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)...
|
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>');
| |
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,...
|
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
|
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 '<' and
'>' 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...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| | |