By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

Delayed SCRIPT Loading

P: n/a
I am trying to load a SCRIPT SRC tag that will result in a
document.write into a specific location in my web page but not call for
the SCRIPT SRC until the end of the page (so as not to delay the page).

The DEFER method won't work because the document.write just starts a
new page.

I was hoping to leave a DIV where the result should end up. Then at
the end of the page create a child of this DIV or another DIV that lays
on top of this DIV and place the results in there.

I can't use the innerHTML property of the DIV as it seems that SCRIPT
tags within that innerHTML are not executed and the result may have
SCRIPT tags.

I also can't use an IFRAME (which is the easy solution) for various
reasons.

I have tried to create a child SCRIPT element of the DIV - but the
document.write still writes at the bottom of the screen.

I also (a lot of requirements, I know) need this to be generic (I won't
know ahead of time the x,y location) and want to use this on a lot of
web pages.

So I want something like this:

Content....

Placeholder for SCRIPT SRC

More content

Call for SCRIPT SRC but results go over (in) the Placeholder
End of Page code.

Thanks for any assistance.

Ed Brandmark

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
eb********@aol.com wrote:
I am trying to load a SCRIPT SRC tag that will result in a
document.write into a specific location in my web page but not call for
the SCRIPT SRC until the end of the page (so as not to delay the page).

The DEFER method won't work because the document.write just starts a
new page.
Then don't use document.write. Use DOM methods to modify your
page.

I was hoping to leave a DIV where the result should end up. Then at
the end of the page create a child of this DIV or another DIV that lays
on top of this DIV and place the results in there.
Reference your location using document.getElementById or similar.
I can't use the innerHTML property of the DIV as it seems that SCRIPT
tags within that innerHTML are not executed and the result may have
SCRIPT tags.


If you insert a script into a page, it will run as if it was
there when the page was loaded. You can also insert a script
with functions, then call them later.

Here's a sample HTML page:

<html>
<head>
<title>play</title>
<script type="text/javascript">
function addScript(){
var h = document.getElementsByTagName('head')[0];
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = 'play.js'
h.appendChild(s);
}
</script>
</head>
<body>
<button onclick="addScript();">Add script</button>
<button onclick="hiMum();">Hi mum</button>
<br>
<span id="outSpan"></span>
</body>
</html>

Save the following as "play.js" in the same directory:

if (document.getElementById && document.createTextNode){
var tx0 = document.createTextNode('hi mum 0');
document.getElementById('outSpan').appendChild(tx0 );
}

function hiMum() {
if (document.getElementById && document.createTextNode){
var o = document.getElementById('outSpan');
o.appendChild(document.createElement('br'));
o.appendChild(document.createTextNode('hi mum 1'));
}
}
Clicking in "Add script" adds the script to the page. When
loaded, it runs and writes "Hi mum 0" to the page. Clicking on
"Hi mum" will added "Hi mum 1" to the page. Clicking on "Hi mum"
before then does nothing (or generates an error).
--
Rob
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.