943,685 Members | Top Members by Rank

Ad:
Feb 18th, 2007
0

window events and javascript objects

Expand Post »
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(){
myObject.showObject();
}

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
Last edited by theLord; Feb 18th, 2007 at 11:41 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theLord is offline Offline
4 posts
since Feb 2007
Feb 20th, 2007
0

Re: window events and javascript objects

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)?
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
nikkiH is offline Offline
79 posts
since Dec 2006
Feb 22nd, 2007
0

Re: window events and javascript objects

Sorry this post was accidental. just hit the enter key by mistake
Last edited by theLord; Feb 22nd, 2007 at 1:01 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theLord is offline Offline
4 posts
since Feb 2007
Feb 22nd, 2007
0

Re: window events and javascript objects

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

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

}
MYObject.prototype.showObject = function(){
//alert('called');
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theLord is offline Offline
4 posts
since Feb 2007
Feb 22nd, 2007
0

Re: window events and javascript objects

I'm not totally sure, but I think you need the new keyword?
This appears to be a functioning example of something similar in that it attaches a prototype function to the window scroll. Perhaps it will help.

http://javascript.internet.com/css/f...r-manager.html
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
nikkiH is offline Offline
79 posts
since Dec 2006
Feb 24th, 2007
0

Re: window events and javascript objects

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'.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theLord is offline Offline
4 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Text field does not enable on refresh
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Multiple styles





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC