The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 17, 2019, 12:09:28 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: ECMAScript (JS) Event Handlers  (Read 3505 times)
nathanr
n00b
*
Offline Offline

Posts: 7


View Profile
« on: October 27, 2007, 12:34:34 PM »

Code:
/* nathanr: Event Handler Functions - Cross Browser Functionality */

function get_event(e) {
/* call: var e = get_event(e);
uniforms event object to make scripting simpler */
if(!e) {
var e = window.event;
}
if (e.srcElement) {
e.target = e.srcElement;
}
if(e.target) {
if (e.target.nodeType == 3) {
e.target = e.target.parentNode;
}
}
if(e.clientX || e.clientY) {
if(document.documentElement) {
try {
e.pageX = e.clientX + document.documentElement.scrollLeft;
e.pageY = e.clientY + document.documentElement.scrollTop;
} catch (error) {

}
} else {
e.pageX = e.clientX + document.body.scrollLeft;
e.pageY = e.clientY + document.body.scrollTop;
}
}
return e;
}

function add_event(event_name, function_name, event_target) {
/* add_event("mousedown", myFunction, document.getElementById('elementId'));
add_event("mouseover", myFunction, false); */
if(!event_target) {
if(document.documentElement) {
event_target = document;
} else {
event_target = document.body;
}
}
if (event_target.attachEvent) {
remove_event(event_name, function_name, event_target);
event_target.attachEvent("on" + event_name, function_name);
} else if(event_target.addEventListener) {
event_target.addEventListener(event_name, function_name, true);
} else {
alert('Your browser does not appear to support attachEvent OR addEventListner; Please either update your current browser or use an alternative.');
}
}

function remove_event(event_name, function_name, event_target) {
/* see add_event comments */
if(!event_target) {
if(document.documentElement) {
event_target = document;
} else {
event_target = document.body;
}
}
if (event_target.detachEvent) {
event_target.detachEvent("on" + event_name, function_name);
} else if(event_target.removeEventListener){
event_target.removeEventListener(event_name, function_name, true);
} else {
alert('Your browser does not appear to support detachEvent OR removeEventListener; Please either update your current browser or use an alternative.');
}
}

function stop_event(e) {
/* stop event propagation, cancel bubble, depending on which browser you are using
you may also want to set the event target to unselectable especially in ie when
using drag operations on A nodes
if(e.target.nodeName == "A") {
e.target.setAttribute("unselectable", true);
} */
if (e.stopPropagation) {
e.stopPropagation();
if(e.preventDefault) {
e.preventDefault();
}
} else {
e.cancelBubble = true;
e.returnValue = false;
}
}

this'll save you a few hours of coding guys Wink
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: October 27, 2007, 12:38:02 PM »

this'll save you a few hours of coding guys Wink

NO LIE. This is one area that I have not written stock classes for the base JS I send out with pages and as such I almost always end up writing the hooks and vectors by hand. These look good N, gonna have to throw them into the testing mix because they'd DEFINITELY help.

Thanks!
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nathanr
n00b
*
Offline Offline

Posts: 7


View Profile
« Reply #2 on: October 27, 2007, 12:46:37 PM »

no problem  Smiley I'm afraid I'm not giving away all my secrets though! just the simple stuff you could right click and cp anyways  D'oh!
Logged

No links in signatures please
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!