A warm Hi to all members to of the Dani Web community!

I am facing a problem with window events in javascript. In my code I am creating an javascript object using a constructor and i have created all its functions using 'prototype' . Here is a sample code

var myObject = new MYObject(param1, param2);

MYObject.prototype.showObject = function(){.......
Now somewhere in my code, i required to add an event handler for the window.onscroll event.
First, i tried by using a member of my object which happens to be a frame as follows :

this.IFrame = document.getElementById('iFrame1');
this.IFrame.parentWindow.onscroll = this.showObject;

but it won't work

then i tried writing an external function as follows:

function callshowObject(){

window.onscroll = callshowObject;
this too doesn't work. :mad:
Is there any way by which i can make my object's function the eventhandler for the window's event?
i would be grateful for any help

10 Years
Discussion Span
Last Post by theLord

You didn't post full enough code for me to test, so please clarify "doesn't work". Do you get an error? Did you check the javascript console (firefox) and/or turn on error messages (IE)?


Sorry this post was accidental. just hit the enter key by mistake


ok here is more detailed code
function MyObject(id,pos)
this.id = id;
this.IFrame = document.getElementById('iFrame1');

this.IFrame.parentWindow.onscroll = this.showObject;

MYObject.prototype.showObject = function(){
this.IFrame.style.position.top = 0;
var myObject = new MYObject;

i checked thru alerts and found the object is created and also the function works fine when it is not part of the object. But once i use prototype and make it part of object, it is not called at all on the window scroll event.
And yes, i used ie and messages are turned on


thanks a lot Nikkih. I got the solution from the url. U were right. The problem is with the keyword. If you want to assign an handler to a window event, the correct syntax is

window.onScroll = new Function(this.id+'.showObject();');

// showObject is the name of the custom event handler
//this.id is the object instance name. In my case its value will be myObject

Note 2 things here. You cannot reference the window your object loads in by anyobject member. ie. I tried
this.IFrame.parentWindow.onscroll but it won't work.
you can refer to the window only by 'window'.
Secondly note the 'F' is captial in 'new Function'. The keyword 'Function' with 'f' capital is different from 'function'.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.