| | |
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
![]() |
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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.
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/
Bored? Visit http://www.kaelisspace.com/
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
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
//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
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
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/
Bored? Visit http://www.kaelisspace.com/
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
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'.
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'.
![]() |
Similar Threads
- Using "variable variables" in Javascript (JavaScript / DHTML / AJAX)
- HELP! With Javascripts Pop-Up Window (JavaScript / DHTML / AJAX)
- Accessing JavaScript objects in the Body Tag (JSP)
- JavaScript's window.opener (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Text field does not enable on refresh
- Next Thread: Multiple styles
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate array automatically beta box bug calendar cart checkbox child codes column createrange() css cursor date debugger decimal design disablefirebug dom download dropdown editor element engine enter error explorer ext file firefox focus forms frameworks getselection google gwt html htmlform ie8 iframe images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent pdf php player post problem programming progressbar prototype redirect regex runtime safari scale scriptlets scroll search select shopping size sql text textarea w3c web website window windowofwords windowsxp wysiwyg \n





