How to use document.write and innerhtml

document.write and innerhtml both the functions are useful to adding the dynamic content in in Document.

Both function are many times used by developer in javascript functions, Ajax calls, Web services and many JS libraries.

So We need understand the similarities and differences and limitation about document.write and innerhtml.

First whenever and where you calls document.write function that more important.

Here I am going to show you how to use the document.write first.

<script type="text/javascript">

var hello_world = function() {
document.write('Wordpressapi is good website'); // print hello world in DOM

var mytext = "Wordpressapi is best website";
document.write(mytext); // print again through variable.


Using above code you can print or execute the so much HTML content in DOM.

Here I am going to show you how to use the innerHTML in DOM.

<script type="text/javascript">
document.getElementById('sample_Div').innerHTML="Wordpressapi is good website"; // add simple text
//document.getElementById('sample_Div').innerHTML="<img src=sample.jpg></img>"; // add image

Note: In your document “sample_Div” id with div need to be present.
put following code in your document.

<div id=\"sample_Div\">
Sample Text

Loading iframe using innerhtml is the best techniqe and solution for web services and cross domain calls.

document.getElementById('sample_Div').innerHTML = "<iframe src=''></iframe>";

Both the techniques are stand for as per situation and use

You may like following Articles!

  • Pingback: uberVU - social comments

  • Smokeless cigarettes

    Admiring the time and effort you put into your blog and detailed info you provide! I am going to bookmark your blog and have my children check up the following generally. Thumbs up!

  • Sunni Finken

    Sweet post.

  • Johnny Paywa

    I REALLY liked your post and blog! It took me a minute bit to find your site…but I bookmarked it. Would you mind if I posted a link back to your post?

  • Florinda Wolfson

    I’ve been checking your blog for a while now, seems like everyday I learn something new :-) Thanks

  • Pete Widrick

    Awesome post. I so good to see someone taking the time to share this information

  • Isreal Tacheny

    Intriguing post. I have been searching for some good resources for solar panels and discovered your blog. Planning to bookmark this one!

  • Felipa Giessinger

    This post makes a lot of sense !

  • Hai Goring

    Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards, Reader

  • Joaquin Pov

    Great read. Thanks for the info!

  • Inge Ridlen

    Good! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?

  • Malcolm Geigel

    I’ve just started off a blog, the knowledge you give on this site has aided me extremely. Thank you for all your time & work.