homepage Welcome to WebmasterWorld Guest from 54.234.0.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
How do I run ONLOAD inside document.write?
irock




msg:4147291
 8:47 am on Jun 5, 2010 (gmt 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>

 

birdbrain




msg:4147324
 11:36 am on Jun 5, 2010 (gmt 0)

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

irock




msg:4147403
 5:05 pm on Jun 5, 2010 (gmt 0)

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

Thanks man.

birdbrain




msg:4147409
 5:18 pm on Jun 5, 2010 (gmt 0)

No problem, you're very welcome. ;)

irock




msg:4147453
 6:55 pm on Jun 5, 2010 (gmt 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>

birdbrain




msg:4147462
 7:19 pm on Jun 5, 2010 (gmt 0)

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

JAB Creations




msg:4148695
 7:56 am on Jun 8, 2010 (gmt 0)

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

L33t_J0rdan




msg:4153428
 7:29 am on Jun 16, 2010 (gmt 0)

Thanks for the explanation, John. Looks like I'm not going to be using
document.write anymore!
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved