-
-
Save chrisl8888/796fe9ba6bb52b97e27a to your computer and use it in GitHub Desktop.
Revisions
-
dciccale revised this gist
Aug 13, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,3 @@ # triggerEvent Cross-browser function to trigger DOM events. -
dciccale created this gist
Aug 13, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # triggerEvent Cross-browser function to trigger DOM element events. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ <!doctype html> <title>Demo</title> <span id="btn">Button</span> <script> // use addEvent cross-browser shim: https://gist.github.com/dciccale/5394590/ var addEvent = function(a,b,c){try{a.addEventListener(b,c,!1)}catch(d){a.attachEvent('on'+b,c)}}; // triggerEvent var triggerEvent = function(c,d,b,a){b=document;b.createEvent?(a=new Event(d),c.dispatchEvent(a)):(a=b.createEventObject(),c.fireEvent("on"+d,a))}; // DOM element var element = document.getElementById('btn'); // callback function var callback = function () { alert('Button clicked!'); }; addEvent(element, 'click', callback); // alert 'Button clicked!' when trigger click event triggerEvent('click', element, callback); </script> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ /** * @param target can be any DOM Element or other EventTarget * @param type Event type (i.e. 'click') * @param doc Placeholder for document * @param event Placeholder for creating an Event */ function (target, type, doc, event) { doc = document; if (doc.createEvent) { event = new Event(type); target.dispatchEvent(event); } else { event = doc.createEventObject(); target.fireEvent('on' + type, event); } }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ function(c,d,b,a){b=document;b.createEvent?(a=new Event(d),c.dispatchEvent(a)):(a=b.createEventObject(),c.fireEvent("on"+d,a))}