Welcome to WebmasterWorld Guest from 54.144.107.83

Forum Moderators: open

Message Too Old, No Replies

How do I run ONLOAD inside document.write?

     
8:47 am on Jun 5, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 2, 2002
posts: 1099
votes: 0


I kinda hope you understand what i'm trying to do here. I have a function that I want to load for iframe, but I am using document.write to display the iframe.

<script type="text/javascript">
//<![CDATA[

var cnetproductname = 'Buffalo';
var id = '32432443';
var type = 'reviews';

document.write('<iframe id="myFrame" frameborder=0 scrolling=no width=100% src="http://www.domain.com/grab.php?productname=' + productname + '&id=' + id + '&type=' + type + '&embed=0" onload="adjustMyFrameSize();" >');

//]]>
</script>
11:36 am on June 5, 2010 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 2, 2003
posts: 948
votes: 4


Hi there irock,

I prefer to use DOM methods rather than document.write. ;)

It seems that it is only IE that will not render the "onload"
So the only work-around that I could think of for that browser was setTimout()

Here is my code...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>untitled document</title>

<script type="text/javascript">

function init(){

cnetproductname='Buffalo';
id='32432443';
type='reviews';

f=document.createElement('iframe');
f.setAttribute('id','myFrame');
f.setAttribute('frameborder',0);
f.setAttribute('scrolling','no');
f.setAttribute('width','100%');
f.setAttribute('src','http://www.domain.com/grab.php?productname='+
cnetproductname+'&id='+id+'&type='+type+'&embed="0"');

f.setAttribute('onload','adjustMyFrameSize()'); [red]/*IE returns "undefined" for "onload" */[/red]

document.getElementById('iframe-container').appendChild(f);

if(isIE=/*@cc_on!@*/false){
setTimeout(function(){adjustMyFrameSize()},5000);
}
}

function adjustMyFrameSize(){
alert('function adjustMyFrameSize is initiated')
}

if(window.addEventListener){
window.addEventListener('load',init,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',init);
}
}

</script>

</head>
<body>

<div id="iframe-container">

</div>

</body>
</html>


Also note that the id selector should never start with a number.
It may well work in javascript coding but will fail in CSS.

birdbrain
5:05 pm on June 5, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 2, 2002
posts: 1099
votes: 0


WOW... I didn't expect someone to provide me with a complete fix. I will try that now.

Thanks man.
5:18 pm on June 5, 2010 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 2, 2003
posts: 948
votes: 4


No problem, you're very welcome. ;)
6:55 pm on June 5, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 2, 2002
posts: 1099
votes: 0


Thanks birdbrain,

I'm sorry, but how do I print out the result? Do I have to put something inside to make this work? Sorry for asking a dumb question.

<div id="iframe-container">

</div>
7:19 pm on June 5, 2010 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 2, 2003
posts: 948
votes: 4


Hi there irock,

Sorry for asking a dumb question.


It's not a dumb question but I do not really sure that I understand it. ;)

I'm sorry, but how do I print out the result? Do I have to put something
inside to make this work? Sorry for asking a dumb question.

To make it work just "Copy & Paste" the code into Notepad and save as
an HTML file.

birdbrain
7:56 am on June 8, 2010 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 26, 2004
posts:3143
votes: 12


Whoa, WHOA!

Don't use document.write EVER!

Using the script element as a child to the body element is exceptionally poor practice. Using document.write which is not a standard (and no, even if they did add it to a standard it's still a heaping pile of...bad) is the quickest way to go jump off a thousand foot cliff with JavaScript.

To write a string of text to an element try the following...

XHTML
<span id="target_span"> </span>


JavaScript
document.getElementById('target_span').firstChild.nodeValue='Hi Mom!';


Keep in mind that if the span does not contain a string (including even an empty space) browsers will have difficulty with this.

- John
7:29 am on June 16, 2010 (gmt 0)

New User

5+ Year Member

joined:June 15, 2010
posts:22
votes: 0


Thanks for the explanation, John. Looks like I'm not going to be using
document.write
anymore!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members