0

I'm doing a cross domain request for IE using the XDomainRequest in this way:

<div id="result"></div>

<script type="text/javascript">
var urlToOpen;
var openxUrl = "http://DOMAIN.com/www/delivery/apu.php";
if ($j.browser.msie && window.XDomainRequest) {
        // Use Microsoft XDR
        var xdr = new XDomainRequest();
        xdr.open("get", openxUrl);
        xdr.onload = function() {
            urlToOpen  = xdr.responseText;
        };

        xdr.send();
    }

$j('#result').html(urlToOpen)
</script>

The code return the correct value, but I want to use the value of the Ajax return in other functions (not only inside the function of xdr.onload), so I need that what is returned with xdr.responseText can be declared as global or something like that.

Example: The last line $j('#result').html(urlToOpen) pretend to assign the value of "urlToOpen" but this does not work. How can I achieve this ?

Saymon
  • 510
  • 9
  • 20

2 Answers2

0

The simple answer here is to use remove the var from xdr and then it becomes a property of the global object. You then will be able to access it anywhere.

I would also namespace it so it doesn't conflict, so name it something like myApp_xdr.

I hope this helps.

bitoiu
  • 6,893
  • 5
  • 38
  • 60
  • No it did not work. I copy here how I edited the code. I get the alert (which mean that the load ran fine). but the value of "test" was not printed which make think that it was not declared like global. if ($j.browser.msie && window.XDomainRequest) { myApp_xdr = new XDomainRequest(); myApp_xdr.open("get", openxUrl); myApp_xdr.onload = function() { test="test"; alert("alert worked"); }; myApp_xdr.send(); } $j('#result').html(test); – Saymon Nov 17 '12 at 21:50
  • You are doing something wrong, please check Chrome Dev Tools and check references as you go, this fiddle I believe proves my point on a solution to your problem: http://jsfiddle.net/bitoiu/78vCH/ – bitoiu Nov 18 '12 at 01:31
0

Solution: Since "xdr.onload" run as an asynchronous way, the problem was that I tried to use the variable "urlToOpen" in the code $j('#result').html(urlToOpen) when this variable was empty due that "xdr.onload" had not finnished of load, so I place the code $j('#result').html(urlToOpen) inside the "xdr.onload" function. It is not the best but worked.

Saymon
  • 510
  • 9
  • 20