Javascript window.location calls getting lost?

I am having some trouble with a bit of code. I have a function that does some stuff to some data, calls a remote system (activating a script on that system and passing in the data), and then makes another call to the same system to activate a different script (which acts on the data saved above). The problem is that the 1st call to the remote system appears to get lost in the execution.

This is being run in Safari, uses jquery; the function is tied to a button click, which is defined in the javascript code with an onclick function (ie it is not defined in the html button definition).

Here's a rough breakdown of the function (cleaned out for viewing purposes - I hope I left enough to make it clear):

function compareJSON() {
    // loop through the objects, testing and changing data
    //  ...

    dataSession=({ //build object for output    });   

    $.each( dataSession.chapters , function( indexC, value ) {  
        //compare objects to some others, testing and changing data
    });

    //  ...

    //Call remote script on other system
    urlString="url://blah.dee.com/Blar?script=SaveJSON&$JSONobject=";
    window.location= urlString + JSON.stringify(dataSession);

   //Call remote script on other system
   window.location="url://blah.dee.com/Blar?script=EditJSON";
}

The last three lines of code are the two calls. It uses the window.location to actually trigger the remote system, passing the data through the URL. But I need BOTH scripts to get called and run. It appears that only the LAST script in the sequence ever gets run. If I switch them around it remains whatever is in last place.

Is there something about the window.location that doesn't actually process until the end of the function?

This script actually used to be a series of separate function calls, but I figured I was running into asynchronous execution that was causing the various script calls to not register. But once I put the code into this single function, it was still happening.

Any clues would be helpful.

Thanks, J


Modifing the value of window.location is reserved exclusively for instances in which you'd like to cause a browser redirect.

It looks like you want to trigger a page request instead. You say you already have jQuery loaded, if so, you can trigger such a request using jQuery.get or a similar function.

For example:

// Loads the myscript.php page in the background
$.get('myscript.php');

// You can also pass data (in the form of an object as the second argument)
$.get('myscript.php', { name: "John", time: "2pm" });
链接地址: http://www.djcxy.com/p/71688.html

上一篇: 使用Mongoose避免在Node.JS中超出调用堆栈大小

下一篇: JavaScript的window.location调用迷路了吗?