troubleshooting Question

Parsing og Javascript in ajax response

Avatar of Batalf
BatalfFlag for United States of America asked on
JavaScript
7 Comments1 Solution3869 ViewsLast Modified:
Hi all

I have ran into a problem with Ajax which I hope you can answer.

First of all, this is my basic Ajax routine

1) I'm using the sack library (http://twilightuniverse.com/resources/code/sack/)

2) I'm making a call to the server and get the answer back and stored in a response property of the js sack object, let's call it

ajax.response;

3) Then I'm inserting this content into a element on the page like this:

document.getElementById('myDiv').innerHTML = ajax.response;

Now, the problem arise in IE because it won't parse javascript content in the response.

So this is my solution....which almost works:

      __evaluateJs : function(obj)
      {
            window.jsCode = new Array();
            var scriptTags = obj.getElementsByTagName('SCRIPT');
            var head = document.getElementsByTagName('HEAD')[0];
            for(var no=0;no<scriptTags.length;no++){                  
                if (scriptTags[no].src){
                    var head = document.getElementsByTagName("head")[0];
                    var scriptObj = document.createElement("script");            
                    scriptObj.setAttribute("type", "text/javascript");
                    scriptObj.setAttribute("src", scriptTags[no].src);             
                    head.appendChild(scriptObj);
            
                }else{                      
                        var code = scriptTags[no].innerHTML;                        
                        window.jsCode[no] = code;
                        setTimeout('eval(window.jsCode[' + no + '])',100);      // Has to wait because we want to make all objects part of the window object(global variables) instead of locale
                }            
            }      
      }

I'm sending document.getElementById('myDiv') as a input to this method. This method parses the <script> tags inside the element and runs an eval() on the innerHTML of each of them.

This executes the javascript code.

But now comes the challenge: functions and variables inside that method are registered as member of the method where the eval() is executed. They become local variables instead of globals. I have tried to solve this by implementing a setTimeout( setTimeout('eval(window.jsCode[' + no + '])',100); ) , but that only seem to work in Firefox.

I have made an example here:

http://www.dhtmlgoodies.com/examples/ajax.html

it includes the file

http://www.dhtmlgoodies.com/examples/ajax_include.html

If it works, a click on the link at the bottom should alert "20" (which it does in FF).

But IE says that "myObj is undefined".

I hope someone can help me with this problem.

Thanks a lot
ASKER CERTIFIED SOLUTION
DarthMod

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros