Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Alternative to .innerHTML ?

Posted on 2006-11-18
2
Medium Priority
?
1,010 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 80 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

688 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