I open a window, and want to perform some calculation on some condition.
Problem is in IE, only first call of test
(from ready
) is performed from new window,
but subsequent calls of test
(set with setTimeout) are executed in parent window and not for new window (i.e. win
).
It works fine in FF.
illustrating code (this code is in parent window):
var win = window.open(some_url, window_name, argument_string);
with (win) {
function test() {
alreadyrunflag += 1;
if (alreadyrunflag < 10) {
window.setTimeout(function() { test(); }, 500);
}else {
//perform calculation
}
}
jQuery(win.document).ready(function() {
alreadyrunflag = 0
test();
});
}
I would strongly recommend against using with
in this situation (and, frankly, almost any other). Instead, simply write win.
everywhere necessary. Like Eric Meyer, I'm not a huge fan of "considered harmful" essays, but this one by Douglas Crockford I'm in full agreement with.
with
modifies the "scope chain" in a way that it takes a seriously advanced knowledge of JavaScript to understand fully. See Crockford's essay for details. It also introduces speed penalties (though granted, while I've heard people rant about them, I doubt the costs are really that high). The effects on maintainability are just unacceptable. Also, you can't use it in the new strict mode (MDC link), which is a useful mode that helps you avoid some common bugs.
In this case, it seems to me that the scope chain should resolve in the way you're expecting (and the way Firefox apparently is doing it), but frankly I'm not shocked at all that IE's JScript implementation doesn't handle it the same way. And/or it may be an issue with which jQuery
ends up getting used (the one in the opening window or the one in the just-opened window; you can't be sure, and it would be timing-dependent...yuck).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With