-1

I have following code in my JavaScript library. My goal is to store the original onscroll function which is the first line of code snippet, and then point onscroll to a new function. However, what I am finding is that wind.origonscroll which is supposed to be the original scroll function is always pointing to the new function in second line.

Question: Is there a way to store the original function in code below, so window.origscroll always points to the function in first line of commented code? I am trying to keep track of what was the original window's onscroll event, which is not related to overriding a function. I do not need to override a function here.

 //window.onscroll = function() {console.log('scrolling done');}
 wind.origonscroll = window.onscroll;
 window.onscroll = function () { window.scrollTo(wind.x, wind.y); };
Sunil
  • 20,653
  • 28
  • 112
  • 197
  • 1
    Possible duplicate of [Overriding a JavaScript function while referencing the original](http://stackoverflow.com/questions/296667/overriding-a-javascript-function-while-referencing-the-original) – huysentruitw Oct 28 '15 at 05:42
  • I am not trying to override a function. the post you mentioned is talking about how to override a function. What I am trying to do is store the original function that the window's onscroll event points to. – Sunil Oct 28 '15 at 05:47
  • 3
    Could you please add a code reproducing your issue, your basic idea [seems to work fine](http://jsfiddle.net/ktoq3hv2/1/). – Teemu Oct 28 '15 at 05:48
  • @Teemu, you mean demo code on Fiddle or similar? – Sunil Oct 28 '15 at 05:52
  • Just a bit of code with which we can reproduce your issue. A couple of additional lines to the post. A fiddle would be a nice extra, but not necessary. – Teemu Oct 28 '15 at 05:56
  • 1
    `I am finding is that wind.origonscroll which is the original scroll function is always pointing to the new function in second line` - you're doing something wrong **somewhere else** (like maybe that piece of code gets executed twice – Jaromanda X Oct 28 '15 at 06:02
  • @Teemu, I have added an extra line to the code to show that there was an original function for window.onscroll, which should be stored in `wind.origonscroll`. – Sunil Oct 28 '15 at 06:03
  • 1
    window.onscroll on load is null (aka there is not a handler defined). What do you mean when you say you want to keep track of the event? The event is the signal that fires when scroll happens, here we are talking about the handler function – Carlo Oct 28 '15 at 06:04
  • I mean the handler function. Couldn't the handler for window.onscroll be already defined by the developer in some code? O are you saying that I should always consider window.onscroll as null for the original window.onscroll handler? – Sunil Oct 28 '15 at 06:05
  • 1
    @Sunil Storing `window.onscroll` works fine, as you can see at my above linked fiddle. As Jaromanda X said, there must be something else in your code ... – Teemu Oct 28 '15 at 06:14
  • @Teemu, I cannot see a link to a demo on fiddle. May be you forgot to post it. – Sunil Oct 28 '15 at 06:28
  • @Teemu, I have the answer, so its ok. Thanks for your help. – Sunil Oct 28 '15 at 06:43
  • @Sunil ??? The words "seems to work fine" in the third comment on this thread is the link. – Teemu Oct 28 '15 at 06:56
  • @Teemu, Thanks. Yes, I see it now. – Sunil Oct 28 '15 at 16:11

1 Answers1

1

If window.onscroll is not null ( if has been defined before) it can be saved away.

Try this:

window.onscroll=function() { console.log('asd') }
var old = window.onscroll
window.onscroll=function() { console.log('gggg') }
old()
Carlo
  • 2,103
  • 21
  • 29