views:

402

answers:

3

The parameter *return_value* contains

   <textarea>{"id":43,"description":"","item_id":28,"callback":"addNewAttachment","filename":"foo.jpg",,"type":"posts","ext":"jpg","size":145}</textarea>

The next code removes the textarea tags in Firefox, Chrome, so the content can be accessed in arr[1]. In IE alert("Failure") is called.

function addAttachment(returned_value) {
    var re = new RegExp ("<textarea>(.+)</textarea>");      
    var arr = re.exec(returned_value);
    if(arr != null && arr.length > 1) {
     var json = eval('(' + arr[1] +')');
    } else {
     alert("Failure");   
    } 
    window[json.callback](json);
}

*returned_value* comes from an ajax call. I use JQuery.

TEST

This does not work either:

var re = new RegExp (/<textarea>(.+)<\/textarea>/);

SOLUTION

The problem was that IE was getting the textarea String uppercased while firefox was getting it lowercase.

The next regular expression solves it.

var re = new RegExp ('<textarea>(.+)</textarea)>','i');
A: 

What IE version do you use? I tested the following code in IE 7 and it worked:

<script>
var x = '<textarea>{"id":43,"description":"","item_id":28,"callback":"addNewAttachment","filename":"foo.jpg",,"type":"posts","ext":"jpg","size":145}</textarea>'

var r = new RegExp('<textarea>(.+)</textarea>');
var a = r.exec(x);
for (var i=1; i<a.length; i++)
 alert(a[i]);
</script>

Edit: I checked with this code in IE7 and it also works. test.xml is a file that contains the string and sits in the folder next to the HTML page with the script. I assume it should also work with a dynamic page that returns the same thing.

<script>
function test(x) {
 var r = new RegExp("<textarea>(.+)</textarea>");
 var a = r.exec(x);
 for (var i=1; i<a.length; i++)
  alert(a[i]);
}

var rq = new XMLHttpRequest();
rq.open("GET", "test.xml", false);
rq.send(null);
test(rq.responseText)
</script>
rslite
You are right, the code works when returned_value is in a string. But I am getting the value returned by the server in an ajax call and there seems to be the issue. Any idea?
Sergio del Amo
What kind of Ajax framework do you use? How is your call made?
rslite
I updated the question. I use JQuery
Sergio del Amo
+3  A: 

Is this a case-sensitive issue? new RegExp(..., "i") might help?

searlea
BTW. Just noticed the JSON object contains two commans: ",," - I think IE will barf on that in the eval() once you get that far.
searlea
+2  A: 

Try using a regex literal:

var r = /<textarea>(.+)<\/textarea>/i;
KooiInc