Solved

Alternative to .innerHTML ?

Posted on 2006-11-18
2
1,001 Views
Last Modified: 2012-08-13
Simple question.  Im using document.createElement("div").  I would like to add HTML and SCRIPT tag contents within this new tag container without using the non-standard .innerHTML property.  How can i acheive this?  I've read of a .nodeValue but it seems biased toward text only input.

Thanks.
0
Comment
Question by:ljaques
2 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 20 total points
ID: 17972732
I wouldn't worry too much about browser-support. If most commong browsers can handle it, why care? Anyway i googled it up for you:

http://www.javascriptkit.com/javatutors/dynamiccontent4.shtml

It seems it is possible, but they do not really seem fond.

You have a div like:
<div id="test">Old content</div>

Normally you would:
<script>document.getElementById("test").innerHTML="new content"</script>

New method would be:
function dynamiccontentNS6(elementid,content)
{
  if (document.getElementById && !document.all)
  {
    rng = document.createRange();
    el = document.getElementById(elementid);
    rng.setStartBefore(el);
    htmlFrag = rng.createContextualFragment(content);
   
    while (el.hasChildNodes())
      el.removeChild(el.lastChild);

    el.appendChild(htmlFrag);
  }
}

I hope the link provides enough information.
Mark
0
 
LVL 6

Expert Comment

by:aescnt
ID: 17975398
> "I would like to add HTML and SCRIPT tag contents within this new tag container"
You can't have an <html> inside another element (like a div). There can only be one <html> in a document. :)

I'm really not sure on what you're trying to do. But for example, if you're doing:

    var test = document.createElement("div");
    test.innerHTML='<script type="text/javascript" src=
"foo.js"></script>';

ADSLMark, I think the asker's concern is logical since IE doesn't fully support .innerHTML on elements that aren't on the tree. (i.e., in this example, the `test` variable hasn't been appended to `document`, so accessing it's .innerHTML in IE might produce errors.)

You can instead do:

    var test = document.createElement("div");
    var myscript = document.createElement("script");
    myscript.setAttribute('type', 'text/javascript'); /* this is the standard way to set attributes */
    myscript.setAttribute('src', 'foo.js');
    myscript.src = 'foo.js'; /* how IE wants attributes to be set */
    myscript.type = 'text/javascript';
    test.appendChild(myscript);

Tested on FF 1.5.0.8.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question