0

The legacy html I have inherited has numerous js calls of the type

Event.observe(window, 'load', function() {
    // Various snippets of code 
});

I would like to be able to fire all of the functions which have been chained to the window's load event AFTER the page has already loaded based upon user's interaction with the page. Ideally, I would do something like

window_on_load_function = window.someCommandHere();
window_on_load_function();

Is there any way to accomplish this? I can use JQuery but solutions without JQuery would also be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sean
  • 1,110
  • 1
  • 14
  • 24
  • 2
    `Event.observe` looks like Prototype.js to me, are you using it? – RienNeVaPlu͢s Nov 04 '13 at 20:32
  • 1
    Yes, Prototype.js is the javascript framework the legacy system was built on – Sean Nov 04 '13 at 20:36
  • So a few answers and links to resources - For firing native events `Event.simulate` will be a good add-on - https://github.com/kangax/protolicious/blob/5b56fdafcd7d7662c9d648534225039b2e78e371/event.simulate.js -- To get all of the event observers I will point you to one of my previous answers - http://stackoverflow.com/a/17978728/341491 – Geek Num 88 Nov 05 '13 at 02:51

2 Answers2

0

I may be misunderstanding your question. Can you not bind the function call to the specific user interaction you're referring to?

IE: if the interaction is them clicking a button then bind it to the onclick of that element. in jQuery would be

$('#ELEMENTID').click(window_on_load_function);
isick
  • 1,467
  • 1
  • 12
  • 23
  • Theoretically yes, I could do this. I suppose my question is, how would I go about attaining the window_on_load_function value? – Sean Nov 04 '13 at 20:37
  • Sorry, misunderstood. Now I think I get your question. This answer might help you. Could fail if prototype treats the window object differently. I don't prefer prototype so I can't say for sure, but worth a shot. http://stackoverflow.com/questions/1422696/update-how-to-find-event-listeners-on-a-dom-node-in-prototype – isick Nov 04 '13 at 21:40
  • I don't prefer prototype either, but it's what the legacy code used :/ – Sean Nov 04 '13 at 22:05
  • Did you try implementing the solution in the link I posted? http://stackoverflow.com/questions/1422696/update-how-to-find-event-listeners-on-a-dom-node-in-prototype It iterates over a Prototype element's listeners so you can call them. In his example answer he does an alert, but you can see how it can be re-figured to execute the function instead. – isick Nov 05 '13 at 02:01
0

You need to find the stored responders of your window object, which are assigned by prototype.js in the following way (see prototype.js 1.7, event.js line 774):

function observeStandardEvent(element, eventName, responder) {
    var actualEventName = getDOMEventName(eventName);
    if (element.addEventListener) {
      element.addEventListener(actualEventName, responder, false);
    } else {
      element.attachEvent('on' + actualEventName, responder);
    }
}

So prototype.js isn't storing your responders on a variable somewhere , but just assigning them on to your element - in this case the window.

Sadly prototype.js has no such thing as jQuerys click (as suggested by isick). But there are plenty of other solutions available, you should have a look at the related question "Trigger an event with Prototype".

especially on Gregs answer. Using his Element.triggerEvent(window, 'load') should do the job. Another (propably even cleaner) solution would be to use Prototypes Event.fire
Neither Gregs answer nor the obvious Event.fire will do the job on window!

But there is a custom built solution from balupton on his question Binding and triggering native and custom events in Prototype he published a gist which seems to be working pretty well!

I made a (dirty) jsfiddle which is proving the possibility of it. Its kind of an inception though.

Community
  • 1
  • 1
RienNeVaPlu͢s
  • 7,442
  • 6
  • 43
  • 77
  • Ya, I'm unable to get Event.fire(window, 'load'); to work, though it definitely makes sense that it would be what I'm looking for. As for your original solution, I don't have the functions which would comprise the responder value, so that approach wouldn't really work unless I was able to access the value which responder should have – Sean Nov 04 '13 at 21:06
  • I saw that fire() can only be used for custom events, I assumed load was a native event and therefore could not be used. I see that there is a link to github on that page, haven't tried that solution yet though – Sean Nov 04 '13 at 21:25