|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
IE innerHTML with STYLE and SCRIPT
Without regard to document.createElement
I know how to create SCRIPT and STYLE elements via createElement and to set their textContent or text or innerHTML etc for the correct browsers. IE wants myScriptNode.text yet textContent and innerHTML fail in IE while they work for Firefox. I want to be able to use innerHTML to create SCRIPT and STYLE nodes in IE Thanks to http://msdn.microsoft.com/workshop/a.../innerHTML.asp I am able to use innerHTML with a SCRIPT tag in harmony: Code:
<body>
<div id="aDiv"></div>
<script type="text/javascript">
var myDiv=document.getElementById("aDiv");
myDiv.innerHTML="<br><script defer>alert('hi')<\/script>"
</script>
</body>
The <br> can be replaced with plain text yet not an HTML comment... I was thinking that maybe this has something to do with the DIV requiring content so I tested out outerHTML and it didn't make a difference in the aspect of getting the SCRIPT to execute. Also if you alert the innerHTML after setting it, you'll notice that innerHTML just skips the SCRIPT tag totally and when you add DEFER and a <br> then it becomes part of the innerHTML... The style tag acts a little differently that it doesn't use DEFER yet still wants a <br> Code:
<body>
<div id="aDiv"></div>
<script type="text/javascript">
var myDiv=document.getElementById("aDiv");
myDiv.innerHTML="<br><style>body{background-color:red;}<\/style>"
alert(myDiv.innerHTML)
</script>
</body>
Anyone understand why the <br> is needed? It's not mentioned as a prerequisite on IE's page.
__________________
Ultimater XMLHttpRequest: Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo Broadening one's horizons: 24ways.org Note I have a bad habit of editing my posts hours at a time and hours later. Last edited by Ultimater; 01-28-2007 at 11:58 PM. Reason: fall-> fail typo |
|
#2
|
|||
|
|||
|
Well, I've always used createElement to make script objects, but I'm not surprised that IE has a problem with innerHTML and script tags. It has been known for a long time that the JScript interpreter has problems when evaluating strings being inserted as HTML code. Also, MS conveniently uses a <br> tag in their example code, whether on purpose or not is yet to be known. On a side note though, you can try the Prototype update() method instead of innerHTML:
http://www.simpltry.com/?p=17 I have neer been a fan or prototype, but I have been messing around with the update() method for the past few weeks, and it seems to be quite useful. |
|
#3
|
||||
|
||||
|
It seems if the SCRIPT element has a parentNode, in IE, and you set the "text" then it executes on the spot.
Code:
<script type="text/javascript">
var s=document.createElement("script");
var d=document.createElement("div");
d.appendChild(s)
s.text="alert('hello')"
s.text="alert('world')"
</script>
__________________
Ultimater XMLHttpRequest: Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo Broadening one's horizons: 24ways.org Note I have a bad habit of editing my posts hours at a time and hours later. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|