java
sql
html
php
python
mysql
database
linux
objective-c
visual-studio
eclipse
perl
facebook
tsql
delphi
apache
api
jsp
postgresql
(Edit See below, this doesn't work.) You can check whether the element has an onload property:
onload
var img = document.createElement('img'); alert("img onload? " + ('onload' in img)); var script = document.createElement('script'); alert("script onload? " + ('onload' in script));
On IE7, I get true for the img, and false for the script.
true
img
false
script
Edit This doesn't work for Firefox. Leaving this just so others don't go down the same path.
I am not sure if this is what you were asking for, but this will let you know if you have a specific method or property available for a given object.
var apple = new Object; apple.load = function() { alert("I am a method.") }; apple.color = "red" function isAvailable(obj, mp) { // obj = element to test for method or property. // mp = name of method or property. if (obj[mp]) { return true; } else { return false; } } if (isAvailable(apple, "color")) { alert("apple object has a 'color' property"); } if (isAvailable(apple, "load")) { alert("apple object has a 'load' method"); }
Edit: Re-worked the answer to show an example.
I've done something like this before; when writing stuff for phone gap on the iphone, depending if you run the app in the simulator or on different versions of the device, you often have different handlers for handling the click of input buttons (and most other things)- so at the top of my script i just do a quick check;
var m_clickEvent = ''; if ( $('input').click != 'undefined') m_clickEvent = 'click' else if ( $('input').tap != 'tap') m_clickEvent = 'tap' else if ( $('input').touchstart!= 'touchstart') m_clickEvent = 'touchstart' else // some kind of error handling..
then i can go ahead and bind my event handler;
$('.myButton').bind(m_clickEvent, function(e) { ... });
Here's an example destilled from the way Modernizr does event detection:
var tmp = document.createElement('script'); tmp.setAttribute('onload', ''); isSupported = typeof tmp.onload == 'function';
One way I've done this in the past is to use the old "for in" loop, and check each key value to see if it starts with "on" (every native event handler I've ever seen starts this way...) So, for example:
var el = document.querySelector("*"), //this is the element (use whatever selector text) elEventHandlers = []; //set up an array to hold 'em all for (var prop in el) //loop through each prop of the element if (prop.substring(0,2)=="on") //if the prop starts with "on" it's an event handler elEventHandlers.push(prop); //so, add it to the array console.log(elEventHandlers); //then dump the array to the console (or whatever)
voila! Now you know what event handlers can be registered on that element!