0

I have a (hopefully) simple question.

I'm using ajax to update a DIV on my current page. Within what's being inserted into the div is a script block that defines a variable. Within FF, I can access the variable, but IE doesn't see the var. Do I need to eval the returned code in some way for IE to recognize the variable?

As a simple example, if my ajax call returns this snippet of code and gets inserted into the div, I get an alert in FF but not in IE:

<script language='JavaScript' type='text/JavaScript'> alert('Foo!'); </script>

2
  • 2
    Probably worth specifying which version of IE you're talking about. IE6, 7 and 8 are all quite different beasts. (Nothing is ever a simple question when IE is involved.) Commented Dec 2, 2010 at 0:28
  • Hi, sorry the question is somewhat vague. I'm using IE8, although I've run across the issue in IE7 as well. Commented Dec 2, 2010 at 0:31

3 Answers 3

2

Yes, the common workaround is to get all the script elements using getElementsByTagName() and then to eval() them.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh how I hate IE ... Can you give me an example? I tried this and still get no alert: var code = cp.document.getElementById('cpanelBody').getElementsByTagName('script'); for (i = 0; i < code.length; i++) { eval(code[i]); }
May I use jQuery? $('#somediv').load('ajax.php', null, function() { eval($('#somediv script').text()); } ); Be aware that the code is executed in the context of that inner function.
Hmm, I'll take a look at that, I'm currently using YUI (long story) but could mix in some jQuery just for kicks. This seems awfully convoluted, though. All I'm trying to do is introduce a set of data that's going to then get processed further -- maybe I'll just return a form with hidden vars to accomplish what I want instead of passing a JS object directly.
1

You should return a JSON object as the result of the AJAX call and then use the json2.js Javascript stuff to evaluate it into a live object.

http://json.org/ has a link to JSON libraries for every language known to man, including Javascript.

1 Comment

Hmm, I'm actually returning JSON already, in the form {'status':'some status','content':'The content getting stuffed into my page'}. If status = 'OK', then I put it in the target div and if it's 'Error', I stuff the returned error message into an error div.
1

The problem might be your JavaScript MIME type - you're using an unconventional type (type='text/JavaScript'):

<script language='JavaScript' type='text/JavaScript'> alert('Foo!'); </script>

You should try this instead:

<script type='text/javascript'> alert('Foo!'); </script>

I ran some tests recently and found that even modern browsers are pretty picky about this MIME type (skip to Additional Test #1), and will refuse to execute the JavaScript if it isn't set correctly: http://davidbcalhoun.com/2010/what-happens-when-we-serve-javascript-with-random-mime-types

If that's not the issue, try adding the script to the document Head:

document.getElementsByTagName('head')[0].appendChild(script);

This is what Google Analytics did until recently, so I would be surprised if it didn't work for you. You shouldn't need to use eval().

According to this page this page, IE is picky with nodes that haven't yet loaded, but the Head is almost guaranteed to have loaded, which is why it shouldn't have the same issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.