window events and javascript objects

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Feb 2007
Posts: 4
Reputation: theLord is an unknown quantity at this point 
Solved Threads: 0
theLord theLord is offline Offline
Newbie Poster

window events and javascript objects

 
0
  #1
Feb 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 79
Reputation: nikkiH is an unknown quantity at this point 
Solved Threads: 4
nikkiH's Avatar
nikkiH nikkiH is offline Offline
Junior Poster in Training

Re: window events and javascript objects

 
0
  #2
Feb 20th, 2007
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)?
Google is your friend. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
Bored? Visit http://www.kaelisspace.com/
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 4
Reputation: theLord is an unknown quantity at this point 
Solved Threads: 0
theLord theLord is offline Offline
Newbie Poster

Re: window events and javascript objects

 
0
  #3
Feb 22nd, 2007
Sorry this post was accidental. just hit the enter key by mistake
Last edited by theLord; Feb 22nd, 2007 at 1:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 4
Reputation: theLord is an unknown quantity at this point 
Solved Threads: 0
theLord theLord is offline Offline
Newbie Poster

Re: window events and javascript objects

 
0
  #4
Feb 22nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 79
Reputation: nikkiH is an unknown quantity at this point 
Solved Threads: 4
nikkiH's Avatar
nikkiH nikkiH is offline Offline
Junior Poster in Training

Re: window events and javascript objects

 
0
  #5
Feb 22nd, 2007
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
Google is your friend. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
Bored? Visit http://www.kaelisspace.com/
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 4
Reputation: theLord is an unknown quantity at this point 
Solved Threads: 0
theLord theLord is offline Offline
Newbie Poster

Re: window events and javascript objects

 
0
  #6
Feb 24th, 2007
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'.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC