473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

(another) infuriating IE bug

If anyone can provide an explanation and/or workaround for this bug in
IE I would be really grateful. Is this a known bug?:

******CODE*******
<html>
<script type="text/javascript">
function mf(){return function(){alert('succes!')}}
</script>
<body>

<form>
<span onclick="f1=mf();alert(f1);setTimeout('f1()',1);"> in form</span>
</form>

<span onclick="f2=mf();alert(f2);setTimeout('f2()',1);"> out of form</span>

</body>
</html>
***************

Thomas
thom [at] schristensen [.] com
Oct 13 '05 #1
9 1548
> "Thomas Christensen" <th**@schristensen.com> wrote:
news:43***********************@dread12.news.tele.d k....

If anyone can provide an explanation and/or workaround for this bug in
IE I would be really grateful. Is this a known bug?:

******CODE*******
<html>
<script type="text/javascript">
function mf(){return function(){alert('succes!')}}
</script>
<body>

<form>
<span onclick="f1=mf();alert(f1);setTimeout('f1()',1);"> in form</span>
</form>

<span onclick="f2=mf();alert(f2);setTimeout('f2()',1);"> out of
form</span>

</body>
</html>
***************

Thomas
thom [at] schristensen [.] com


<script type="text/javascript">
var f1;
var f2;
function mf(){return function(){alert('succes!')}}
</script>

--
BootNic Thursday, October 13, 2005 2:13 PM

Every time I close the door on reality it comes in through the windows.
*Jennifer Unlimited*
Oct 13 '05 #2
Thanx! I kinda knew. But in my application I'll probably have to
hardcode the object-reference in the inline setTimeout... And handle
cancellations... But why this behaviour?!

BootNic wrote:
"Thomas Christensen" <th**@schristensen.com> wrote:
news:43***********************@dread12.news.tele .dk....

If anyone can provide an explanation and/or workaround for this bug in
IE I would be really grateful. Is this a known bug?:

******CODE*******
<html>
<script type="text/javascript">
function mf(){return function(){alert('succes!')}}
</script>
<body>

<form>
<span onclick="f1=mf();alert(f1);setTimeout('f1()',1);"> in form</span>
</form>

<span onclick="f2=mf();alert(f2);setTimeout('f2()',1);"> out of
form</span>

</body>
</html>
***************

Thomas
thom [at] schristensen [.] com

<script type="text/javascript">
var f1;
var f2;
function mf(){return function(){alert('succes!')}}
</script>

Oct 13 '05 #3
On Thu, 13 Oct 2005 20:23:56 +0200, Thomas Christensen
<th**@schristensen.com> wrote:
Thanx! I kinda knew. But in my application I'll probably have to
hardcode the object-reference in the inline setTimeout... And handle
cancellations... But why this behaviour?!


because setTimeout executes in the global scope, it doesn't execute in
the scope it's called in.

This is not an IE bug.

Jim.
Oct 13 '05 #4
Works fine in Mozilla. And it's only when enclosed in a <form></form> it
won't work in IE. So <form> is another scope than everything else in IE?
I suspected as much, but don't get it...

Sorry if I'm being daft, but I'm back to coding client stuff after a few
years... Thanks for your reply!

Jim Ley wrote:
On Thu, 13 Oct 2005 20:23:56 +0200, Thomas Christensen
<th**@schristensen.com> wrote:

Thanx! I kinda knew. But in my application I'll probably have to
hardcode the object-reference in the inline setTimeout... And handle
cancellations... But why this behaviour?!

because setTimeout executes in the global scope, it doesn't execute in
the scope it's called in.

This is not an IE bug.

Jim.

Oct 13 '05 #5
JRS: In article <43***********************@dread12.news.tele.dk> , dated
Thu, 13 Oct 2005 19:36:03, seen in news:comp.lang.javascript, Thomas
Christensen <th**@schristensen.com> posted :
If anyone can provide an explanation and/or workaround for this bug in
IE I would be really grateful. Is this a known bug?:


The code does what it should do.

The bug is in your expectation of what it should do; but you have not
explained what that might be.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 14 '05 #6
Thomas Christensen wrote:
If anyone can provide an explanation and/or workaround for this bug in
IE I would be really grateful. Is this a known bug?:

******CODE*******
<html>
<script type="text/javascript">
function mf(){return function(){alert('succes!')}}
</script>
<body>

<form>
<span onclick="f1=mf();alert(f1);setTimeout('f1()',1);"> in form</span>
</form>

<span onclick="f2=mf();alert(f2);setTimeout('f2()',1);"> out of form</span>

</body>
</html>
***************

Solution. If you add the created function to an object in the global
scope it works fine. I still don't understand why IE make the
<form></form> it's own scope, but that's just me:

<script type="text/javascript">
function mf(){return function(){alert('succes!')}}
</script>
<body>

<form>
<span onclick="mf.f1=mf();alert(mf.f1);setTimeout('mf.f1 ()',1);">in
form</span>
</form>

<span onclick="mf.f2=mf();alert(mf.f2);setTimeout('mf.f2 ()',1);">out
of form</span>
Oct 15 '05 #7
Dr John Stockton wrote:
JRS: In article <43***********************@dread12.news.tele.dk> , dated
Thu, 13 Oct 2005 19:36:03, seen in news:comp.lang.javascript, Thomas
Christensen <th**@schristensen.com> posted :

If anyone can provide an explanation and/or workaround for this bug in
IE I would be really grateful. Is this a known bug?:

The code does what it should do.

The bug is in your expectation of what it should do; but you have not
explained what that might be.

I don't think the code does what it should do. The setTimout can't see
f2 when enclosed in a form even though f2 is created inside the same
form. Maybe I don't understand the DOM, but I can't see why <form>
should be an individual scope when no other html-blocks is.

The code in it's actual implementation is used to hide a popup that is
dynamically created, but that's not the point. It's the scope-thing that
annoy me.
Oct 15 '05 #8
Thomas Christensen wrote:
If anyone can provide an explanation and/or workaround for
this bug in IE I would be really grateful.
I think this is a bug in IE, at least in ECMAScript terms it is a bug.
It might be argued that the functions created from the values of
intrinsic event attributes are in the realm of host objects and so can
exhibit any behaviour that the browser authors want them to. And it has
been demonstrated that the functions created from intrinsic even vales
have some extremely odd characteristics in their (apparently dynamic)
scope chain assignment.
Is this a known bug?:
I don't know about it being a 'known bug' as such, it is an area where
odd behaviour is expected on IE browsers.
******CODE*******
<html>
<script type="text/javascript">
function mf(){return function(){alert('succes!')}}
</script>
<body>

<form>
<span onclick="f1=mf();alert(f1);setTimeout('f1()',1);"> in form</span>

<snip>

The question that I asked first is where is - f1 - in the scope chain.
Inserting - alert(f1) - prior to the assignment produces a - f1 is
undefined error - so f1 is not on the scope chain prior to its
assignment. Thus the assignment must add it as a property of some object
on the scope chain.

It is not a property of the global object else the - setTimeout - would
not error. But replacing the content of the existing - alert(f1) -
with:-

this.f1 // undefined - it is not a property of the SPAN element.
document.f1 // undefined - it is not a property of the document.
document.documentElement.f1 // undefined - it is not a property
// of the HTML element.
document.body.f1 // undefined - it is not a property of the BODY
// element.
docuemnt.forms[0].f1 // undefined - it is not a property of the FORM.

- leaves only one apparent candidate from ECMAScript for the object to
which the property is added, and that is the Variable/Activation object
of the internally generated even handling function (the only object that
cannot actually be examined). However, this can be ruled out by
replacing the form with:-

<form>
<span onclick="f1=mf();alert(f1);">in form</span><br>
<span onclick="alert(f1);">second test</span>
</form>

- where clicking "in form" adds the property to some object and then
clicking "second test" alerts the function string. Thus whichever object
has had the property added it is not an object inside the execution
context of the first even handling function, else the second would not
be able to see the - f1 - property on its scope chain.

Now, we know that there is no - f1 - property on the scope chain prior
to the assignment, and we know that an unqualified identifier (- f1 - in
this case) would be resolved against the scope chain and if it did not
exist an internal Reference type with a null "Base" object would be
returned (ECMA 262 3rd edition; Section 10.1.4). And we know that the
ECMA 262 specified internal - PutValue - function will react to
instructions to assign to a Reference with a null "Base" object by using
the global object as the object on which to call the [[Put]] method
(ECMA 262 3rd Edition; section 8.7.2).

So the expected behaviour of the assignment - f1 = mf(); -, where an
'f1' property does not already exist on any object on the scope chain,
is to create a new property of the global object called 'f1' and assign
the value to that. But that is not what is happening.

The only reasonable explanation for the behaviour demonstrated is that
for functions created form intrinsic event handling attributes inside
forms a different 'global' object masks the real global object during
assignments, and subsequent retrievals. The addition of a second form:-

<form>
<span onclick="f1=mf();alert(f1);">in form</span><br>
</form>

<form>
<span onclick="alert(f1);">second test</span>
</form>

- demonstrates that this additional 'global' object is form-specific, as
clicking on 'second test' in the second form, after the use of 'in form'
in the first, again produces the - f1 is undefined - error.

In ECMAScript terms the behaviour demonstrated is incorrect, in an
actual environment, such as IE, and in relation to the behaviour of
host-provided objects, it is much more questionable whether this would
qualify as a bug or a feature. On the other hand, with IEs problems with
circular references having an additional and inaccessible 'global'
object in the system may be extremely problematic.

The possible "workaround"s (incidentally, last week I discovered that in
French there is no word for "workaround"; did they never workaround? Is
it really an alien concept?) may be to follow general programming advice
and not be in the business of trying to create global variables on the
fly, or using global variables where their use could be avoided. In this
case, modern browsers allow - setTimeout - to take a function reference
as its first argument instead of a string of code, so:-

onclick="setTimeout(mf(),1);"

- would get the job done without any need to assign a value to any
object's property. (That is not without its issues as even browsers as
recent as, at lest some early, Safari versions do not understand
function references in this context, though there is a workaround for
that also.)

Then there is the general advice that if you are going to be using
global variable they should all be _explicitly_ declared in the global
scope. Doing so in your example:-

<html>
<script type="text/javascript">
var f1, f2; /*<-- Declare f1 and f2 as global variables. */
function mf(){return function(){alert('succes!')}}
</script>
<body>
<form>
<span onclick="f1=mf();alert(f1);setTimeout('f1()',1);"> in form</span>
</form>
<span onclick="f2=mf();alert(f2);setTimeout('f2()',1);"> out of form
</span>
</body>
</html>

- prevents the - setTimeout - in "in form" from erroring. As now when
the unqualified identifier - f1 - is referenced in the assignment the
internal Reference type that results has the global object explicitly
assigned to its "Base" property and so there is no ambiguity in which
object should have its [[Put]] method used for the assignment. The
assigned value ends up on the global object as initially expected.

This is the irony in asking if this is a 'known bug', because while I
had observed that the event handling functions IE generates form
intrinsic event attribute values have some extremely strange
characteristics (particularly if you start moving them around in the DOM
or executing them in the global scope) I had never observed this
particular behaviour. Because I habitually minimise the number of global
variables I use, and I explicitly declare those that I do use. Both in
accordance with what are widely held "Best Practices" in javascript
authoring, and both coincidentally acting to avoid provoking this issue.

There will be a strong tendency for individuals who know enough to
effectively analyse this problem to have also learnt (possibly the hard
way) to apply sufficient "Best Practices" to their code to never
encounter the issue themselves. You did the right thing in providing a
specific test-case that demonstrates the issue in isolation. Others
assert that they have issues but in never demonstrating them find their
clams being ignored as probably resulting from erroneous coding that can
only be fruitlessly guessed at, as it cannot be seen.

Richard.

Oct 16 '05 #9
Hi Richard

Thanx a lot for your thourough answer, which sums ups my suspections in
a way I couldn't hope to achieve.

My problem was that I needed to add a setTimout on a function with a
reference to the object (which was dynamically created) in which the
setTimout occurred. And I could not get

<span onclick="setTimout(mf(this),100)">

to work for obvious reasons. I now there are workarounds (incidentally
we don't really have a word for workaround in Danish either, so we use -
"workaround") but I don't like workarounds, so...

As I posted earlier I solved the problem in a for me elegant way(?) by
adding the created fundctions to a pseudo namespace (a global function -
in this case the mf function). So:

mf.f1=mf(this);setTimeout('mf.f1()',100);

And that works fine. And better than declaring "f1" as a variable in the
global scope as I in the actual case didn't know how many
derived/created functions I would need.

Thanx
Thomas

Richard Cornford wrote:
Thomas Christensen wrote:
If anyone can provide an explanation and/or workaround for
this bug in IE I would be really grateful.

I think this is a bug in IE, at least in ECMAScript terms it is a bug.
It might be argued that the functions created from the values of
intrinsic event attributes are in the realm of host objects and so can
exhibit any behaviour that the browser authors want them to. And it has
been demonstrated that the functions created from intrinsic even vales
have some extremely odd characteristics in their (apparently dynamic)
scope chain assignment.

Is this a known bug?:

I don't know about it being a 'known bug' as such, it is an area where
odd behaviour is expected on IE browsers.

******CODE*******
<html>
<script type="text/javascript">
function mf(){return function(){alert('succes!')}}
</script>
<body>

<form>
<span onclick="f1=mf();alert(f1);setTimeout('f1()',1);"> in form</span>


<snip>

The question that I asked first is where is - f1 - in the scope chain.
Inserting - alert(f1) - prior to the assignment produces a - f1 is
undefined error - so f1 is not on the scope chain prior to its
assignment. Thus the assignment must add it as a property of some object
on the scope chain.

It is not a property of the global object else the - setTimeout - would
not error. But replacing the content of the existing - alert(f1) -
with:-

this.f1 // undefined - it is not a property of the SPAN element.
document.f1 // undefined - it is not a property of the document.
document.documentElement.f1 // undefined - it is not a property
// of the HTML element.
document.body.f1 // undefined - it is not a property of the BODY
// element.
docuemnt.forms[0].f1 // undefined - it is not a property of the FORM.

- leaves only one apparent candidate from ECMAScript for the object to
which the property is added, and that is the Variable/Activation object
of the internally generated even handling function (the only object that
cannot actually be examined). However, this can be ruled out by
replacing the form with:-

<form>
<span onclick="f1=mf();alert(f1);">in form</span><br>
<span onclick="alert(f1);">second test</span>
</form>

- where clicking "in form" adds the property to some object and then
clicking "second test" alerts the function string. Thus whichever object
has had the property added it is not an object inside the execution
context of the first even handling function, else the second would not
be able to see the - f1 - property on its scope chain.

Now, we know that there is no - f1 - property on the scope chain prior
to the assignment, and we know that an unqualified identifier (- f1 - in
this case) would be resolved against the scope chain and if it did not
exist an internal Reference type with a null "Base" object would be
returned (ECMA 262 3rd edition; Section 10.1.4). And we know that the
ECMA 262 specified internal - PutValue - function will react to
instructions to assign to a Reference with a null "Base" object by using
the global object as the object on which to call the [[Put]] method
(ECMA 262 3rd Edition; section 8.7.2).

So the expected behaviour of the assignment - f1 = mf(); -, where an
'f1' property does not already exist on any object on the scope chain,
is to create a new property of the global object called 'f1' and assign
the value to that. But that is not what is happening.

The only reasonable explanation for the behaviour demonstrated is that
for functions created form intrinsic event handling attributes inside
forms a different 'global' object masks the real global object during
assignments, and subsequent retrievals. The addition of a second form:-

<form>
<span onclick="f1=mf();alert(f1);">in form</span><br>
</form>

<form>
<span onclick="alert(f1);">second test</span>
</form>

- demonstrates that this additional 'global' object is form-specific, as
clicking on 'second test' in the second form, after the use of 'in form'
in the first, again produces the - f1 is undefined - error.

In ECMAScript terms the behaviour demonstrated is incorrect, in an
actual environment, such as IE, and in relation to the behaviour of
host-provided objects, it is much more questionable whether this would
qualify as a bug or a feature. On the other hand, with IEs problems with
circular references having an additional and inaccessible 'global'
object in the system may be extremely problematic.

The possible "workaround"s (incidentally, last week I discovered that in
French there is no word for "workaround"; did they never workaround? Is
it really an alien concept?) may be to follow general programming advice
and not be in the business of trying to create global variables on the
fly, or using global variables where their use could be avoided. In this
case, modern browsers allow - setTimeout - to take a function reference
as its first argument instead of a string of code, so:-

onclick="setTimeout(mf(),1);"

- would get the job done without any need to assign a value to any
object's property. (That is not without its issues as even browsers as
recent as, at lest some early, Safari versions do not understand
function references in this context, though there is a workaround for
that also.)

Then there is the general advice that if you are going to be using
global variable they should all be _explicitly_ declared in the global
scope. Doing so in your example:-

<html>
<script type="text/javascript">
var f1, f2; /*<-- Declare f1 and f2 as global variables. */
function mf(){return function(){alert('succes!')}}
</script>
<body>
<form>
<span onclick="f1=mf();alert(f1);setTimeout('f1()',1);"> in form</span>
</form>
<span onclick="f2=mf();alert(f2);setTimeout('f2()',1);"> out of form
</span>
</body>
</html>

- prevents the - setTimeout - in "in form" from erroring. As now when
the unqualified identifier - f1 - is referenced in the assignment the
internal Reference type that results has the global object explicitly
assigned to its "Base" property and so there is no ambiguity in which
object should have its [[Put]] method used for the assignment. The
assigned value ends up on the global object as initially expected.

This is the irony in asking if this is a 'known bug', because while I
had observed that the event handling functions IE generates form
intrinsic event attribute values have some extremely strange
characteristics (particularly if you start moving them around in the DOM
or executing them in the global scope) I had never observed this
particular behaviour. Because I habitually minimise the number of global
variables I use, and I explicitly declare those that I do use. Both in
accordance with what are widely held "Best Practices" in javascript
authoring, and both coincidentally acting to avoid provoking this issue.

There will be a strong tendency for individuals who know enough to
effectively analyse this problem to have also learnt (possibly the hard
way) to apply sufficient "Best Practices" to their code to never
encounter the issue themselves. You did the right thing in providing a
specific test-case that demonstrates the issue in isolation. Others
assert that they have issues but in never demonstrating them find their
clams being ignored as probably resulting from erroneous coding that can
only be fruitlessly guessed at, as it cannot be seen.

Richard.

Oct 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: luu duong | last post by:
I know this is probably easy but here is the details. I have an asp page that is not inside a frameset. I want to post data to another asp page that is inside a frameset. So firstpage.asp has...
6
by: anon | last post by:
Post Forwarding question...... For this control below, <asp:Button runat="server" PostTargetUrl="page2.aspx" /> The Attribute: PostTargetUrl="page2.aspx" Is this PostTargetUrl Attribute...
5
by: Daniel Tan | last post by:
Are there anyway to copy rows of records from one query to another query and then hide the records in source query ? Pls advise. Thanks. Regards, Daniel
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
3
by: qwerty | last post by:
I´m new to ASP.Net. My workmate has some experience with it. He claimed that in ASP.Net working with frames is much simpler than it was ASP. I asked explanation but he couldn't give me such. (a...
17
by: Rabbit | last post by:
Hi, On my 1st page, i have a function which gets a new ID value and need to transfer to another immediately. which I want to get in 2nd page using Request.form("txtID"), but doesn't work, the...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
3
by: Sin Jeong-hun | last post by:
It seems like the Protect() uses the Windows accout information to encrypt data. If I know the user name and the password, can I decrypt it on another PC? If it is not, how about the exported key?...
7
by: Clive Dixon | last post by:
Sometimes when I'm typing the literal 'null', Intellisense decides to automatically change it to 'Nullable'. Is there any way anyone knows of stopping it doing this, apart from disabling...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.