www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 01-28-2007, 04:05 PM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,827
    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>
    Yet removing the <br> stops it from working...
    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>
    Remove the <br> and it won't work...

    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
    Reply With Quote
      #2  
    Old 01-28-2007, 04:56 PM
    konithomimo konithomimo is offline
    Registered User
     
    Join Date: Jan 2005
    Posts: 3,071
    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.
    Reply With Quote
      #3  
    Old 01-29-2007, 07:13 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,827
    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>
    When the "text" or "parentNode" changes it fires up.
    __________________
    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.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:26 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.