[Greasemonkey] Check COinS against OpenURL Resolver via
GM_xmlhttpRequest
Godmar Back
godmar at gmail.com
Thu Dec 29 14:08:50 EST 2005
I'm wondering if an alternative and potentially cleaner way would be
to add the variable whose value must be captured as a property to the
object passed to GM_xmlhttprequest(), as in:
GM_xmlhttprequest({
coinstag: e,
onload: function (result) {
... this.coinstag.title.replace(...)
}
})
In this way, it's explicit what you're capturing which might be
slightly less so in Aaron's approach (?).
- Godmar
On 12/29/05, Aaron Boodman <zboogs at gmail.com> wrote:
> This is a common problem with async javascript.
>
> You are in a loop, shooting off a request for each item. Each request
> does fire, but by the time any of them return the loop has completed.
> The variables used inside the loop still exist, but they have the
> values they had on the last iteration.
>
> So in this case, when each of the requests returns /e/ has the value
> it had on the last iteration of the loop.
>
> The way to fix this is to call an actual function in the loop, passing
> it arguments. In contrast to the local variables in a loop, each set
> of parameters to a function call is separate. So like this:
>
> for (var i = 0; i < whatever; i++) {
> doSomething(elms[i]);
> }
>
> function doSomething(elm) {
> GM_xmlhttprequest({
> onload: function() {
> // you can use elm here
> }
> });
> }
>
> - a
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>
More information about the Greasemonkey
mailing list