Tuesday, February 19th, 2008

Cursing with JavaScript: Three Random Tips

Category: Browsers, JavaScript, Tip

I ran across a few posts in a row in my news reader that were each mini tips that involved Prototype, Firefox, and IE.

Prototype Custom Events

Howard Lewis Ship is making sure that Tapestry 5 will have 50% more Ajax, and has been playing with Prototype. He found a nagging bug to do with a ‘:’:

Here’s an important note: custom events, used with the fire() function must contain a colon. this.form.fire("prepareforsubmit") will do nothing. this.form.fire("form:prepareforsubmit") will properly invoke observers. This is not yet documented, though the ironically named blog Painfully Obvious clued me in.

Firefox 3 Array Insanity

Alex spent some time that I bet he wishes he could get back as he proved that he wasn’t insane as he ran this in Firefox 3:


  1. >>> typeof []
  2. "object"
  3. >>> var a = [];
  4. >>> var b = new Array();
  5. >>> a.constructor == b.constructor
  6. false
  7. >>> c = [];
  8. []
  9. >>> c.constructor == a.constructor
  10. true
  11. >>> d = new Array();
  12. []
  13. >>> b.constructor == d.constructor;
  14. true
  15. >>> b.constructor == a.constructor;
  16. false

IE Popups

Dushan Hanuska of Atlassian has posted about getting IE to remember its own position:

We try to build user friendly applications as much as we can. As a convenience this little pop-up has a remember-my-position feature. Position (and size) of the window is stored in a browser cookie. This feature works fine on most browsers, however Internet Explorer has some quirks. To be more specific, IE pushes this window off its original position each time this window is opened.

Further investigation of this behavior revealed that this offset happens only if the window’s position is saved. Unfortunately, the JavaScript code was written to re-save window’s position each time this window was popped up, even if it did not move a pixel. Alright, that was fixed quickly. Now the problem was to figure out why does the window jump when its position is saved.

It seems that the offset happens when the window is moved to x,y (window.moveTo(x,y)) and then asked for its x and y (window.screenLeft and window.screenTop). Hence the following code will move the window diagonally down and to the right ten times.

for (var i=0;i<10;i++) window.moveTo(window.screenLeft,window.screenTop);

Did I mention that IE is broken? No? IE is broken. You don’t believe?! Figure this! When you call window.moveTo(x,y) the window dutifully follows your command and moves the upper left corner of the window to x and y on the screen. But when you ask it for its position it will try to fool you by giving you incorrect co-ordinates. And it will succeed. The window.screenLeft and window.screenTop is the only way to get the position of a window on Internet Explorer.

The reason for this offset comes from the difference in position of the browser window (outer window) and the document window (inner window).

The solution? Don’t use pop-up windows. Problem solved.

Posted by Dion Almaer at 7:02 am

3.3 rating from 19 votes


Comments feed TrackBack URI

Regarding the Prototype custom event issue, we’re currently working on a new documentation system for Prototype which should be out pretty soon. So stay tuned.

Comment by Tobie Langel — February 19, 2008

regarding the array insanity, this problem only occurs using Firebug, so it seems a firebug problem!

Comment by Wolfram Kriesing — February 19, 2008

Leave a comment

You must be logged in to post a comment.