1. #1
    Ext User
    Join Date
    Mar 2007
    Posts
    321
    Vote Rating
    0
    jbowman is on a distinguished road

      0  

    Default loadScripts using eval instead of appending to dom

    loadScripts using eval instead of appending to dom


    Ok, I don't want to get into the debate as to whether or not using eval is the correct thing to do. I did this more as an exercise to help me understand javascript better. This patch replaces the portion of element.update that appends inline javascript in the string supplied to the head element of the document, with an eval statement that runs on the string. References to javascript source files should still be appended to the head of the document, though I haven't tested it.

    I've tested this in ie6 firefox 1.5.0.8.

    To use this, you would just run this javascript after you load the yui-ext.js file.

    Code:
    YAHOO.ext.Element.prototype.update = function(html, loadScripts){
            this.dom.innerHTML = html;
            if(!loadScripts) return this;
            
            var dom = this.dom;
            var _parseScripts = function(){
                var s = dom.getElementsByTagName("script");
                var docHead = document.getElementsByTagName("head")[0];
                
                //   For browsers which discard scripts when inserting innerHTML, extract the scripts using a RegExp
                if(s.length == 0){
                    var re = /(?:<script.*(?:src=[\"\'](.*)[\"\']).*>.*<\/script>)|(?:<script.*>([\S\s]*?)<\/script>)/ig; // assumes HTML well formed and then loop through it.
                    var match;
                    while(match = re.exec(html)){
                         var s0 = document.createElement("script");
                         if (match[1])
                         {
                            s0.src = match[1];
    	                    docHead.appendChild(s0);
    	                 }
                         else if (match[2])
                           eval(match[2]);
                         else
                              continue;
                    }
                } else {
                  for(var i = 0; i < s.length; i++){
                     var s0 = document.createElement("script");
                     s0.type = s[i].type;
                     if (s[i].text) {
                        eval(s[i].text);
                     } else {
                        s0.src = s[i].src;
    	                docHead.appendChild(s0);
                     }
                  }
                }
            }
            // set timeout to give DOM opportunity to catch up
            setTimeout(_parseScripts, 10);
            return this;
        }

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    17
    jack.slocum will become famous soon enough jack.slocum will become famous soon enough

      0  

    Default


    Joe, this is similar to what I am doing in the latest code, although there is no more inserting into the html body and fetching the tags. The reason is because IE7 discards some scripts and not others.

Similar Threads

  1. Element.update() and loadScripts...
    By Animal in forum Ext 1.x: Bugs
    Replies: 11
    Last Post: 20 Apr 2007, 3:38 PM
  2. appending an event listener
    By hicker in forum Ext 1.x: Help & Discussion
    Replies: 2
    Last Post: 23 Feb 2007, 10:41 PM
  3. understanding loadScripts
    By jbowman in forum Community Discussion
    Replies: 1
    Last Post: 17 Nov 2006, 9:20 AM
  4. eval-ing json
    By christocracy in forum Community Discussion
    Replies: 6
    Last Post: 19 Oct 2006, 6:45 AM

Thread Participants: 1