thanks for your help!
in the meanwhile I could arrange my code to work by myself. I will
check it with your suggestions.
to be sure that the id remains unique i will add a incremental value to
it at its end.
the following has been tested on safari, firefox and iex. works for
ajax-requests too just recalling traverseTags(aj axresponse).
if you just can shake your head and get bad headaches upon this
solution don't hesitate to let me know!! ;)
<script type="text/javascript">
this.onload = init;
function sali (e){
alert('done' + this.params.toS tring());
}
function hoi(e){
alert('done' + this.params.toS tring());
}
//returns function for a function string
function getFunction(fun ctionstr){
if(functionstr == 'sali'){
return sali;//function sali
}
else if(functionstr == 'hoi'){
return hoi;//function hoi
}
else{
return false;
}
//etc.
}
function init(){
traverseTags(do cument);
}
//if some browser does not know the Node object
if (!window.Node) {
var Node = { // If there is no Node object, define one
ELEMENT_NODE: 1, // with the following properties and values.
ATTRIBUTE_NODE: 2, // Note that these are HTML node types only.
TEXT_NODE: 3, // For XML-specific nodes, you need to add
COMMENT_NODE: 8, // other constants here.
DOCUMENT_NODE: 9,
DOCUMENT_FRAGME NT_NODE: 11
}
}
//stores all possible events
var arrEvents = new Array('onclick' , 'ondblclick', 'onmousedown',
'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout', 'onkeypress',
'onkeydown', 'onkeyup');
//array function to check for a certain value in an array. call:
array.contains( value)
Array.prototype .contains = function(value) {
for (var i = 0; i < this.length; i++) {
if (this[i] == value) return true;
}
return false;
}
//traversing the tags i thereby could assign the function name:
function traverseTags(n) {
var children = n.childNodes;
if(n.nodeType == Node.ELEMENT_NO DE && n.attributes.le ngth 0){
for(var i = 0; i < n.attributes.le ngth; i++){
if(n.attributes[i].nodeName == 'id' &&
isEventTag(n.at tributes[i].nodeValue)){// && n.hasAttributes () not iex
var call = new
callerObject(is EventTag(n.attr ibutes[i].nodeValue));
var eve = call.getEvent() ;
var funct = call.getFunctio n();
var params = call.getParams( );
addEvent(n, eve, getFunction(fun ct), false);//needs to be an
addEvent function which contains the this reference for iex.
n.params = params;
break;
}
}
}
for(var i=0; i < children.length ; i++) { // Loop through the
children
traverseTags(ch ildren[i]);
}
}
//checks if the id attribute is a event
function isEventTag(idAt tribute){
var spliter = idAttribute.spl it('_');//spliter is the array with all
parameters form the id attribute: e.g.
onclick_somefun ction_sometag_p aram1_param2_pa ram3
if(arrEvents.co ntains(spliter[0])) return spliter;
return false;
}
//parses the array
function callerObject(ca llerArray){
var event = callerArray[0];//der event
var funct = callerArray[1];//die function
var callback_obj = null;
var paramArray = new Array();
for(var i = 0; i < callerArray.len gth-2; i++){
paramArray[i] = callerArray[i+2];
}
this.getFunctio n = function(){
return funct;// Function(funct + '(' + paramArray.toSt ring() +
');');
}
this.getParams = function(){
return paramArray;
}
this.getEvent = function(){
return getEventOfStrin g(event);
}
function getEventOfStrin g(eventstr){//private
if(eventstr == 'onclick'){
return 'click';
}
else if(eventstr == 'ondblclick'){
return 'dblclick';
}
else if(eventstr == 'onmousedown'){
return 'mousedown';
}
else if(eventstr == 'onmouseup'){
return 'mouseup';
}
else if(eventstr == 'onmouseover'){
return 'mouseover';
}
else if(eventstr == 'onmousemove'){
return 'mousemove';
}
else if(eventstr == 'onkeypress'){
return 'keypress';
}
else if(eventstr == 'onkeydown'){
return 'keydown';
}
else if(eventstr == 'onkeyup'){
return 'keyup';
}
else{
return false;
}
}
}
// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
//
http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(elemen t, type, handler) {
if (element.addEve ntListener) {
element.addEven tListener(type, handler, false);
} else {
// assign each event handler a unique ID
if (!handler.$$gui d) handler.$$guid = addEvent.guid++ ;
// create a hash table of event types for the element
if (!element.event s) element.events = {};
// create a hash table of event handlers for each element/event pair
var handlers = element.events[type];
if (!handlers) {
handlers = element.events[type] = {};
// store the existing event handler (if there is one)
if (element["on" + type]) {
handlers[0] = element["on" + type];
}
}
// store the event handler in the hash table
handlers[handler.$$guid] = handler;
// assign a global event handler to do all the work
element["on" + type] = handleEvent;
}
};
// a counter used to create unique IDs
addEvent.guid = 1;
function removeEvent(ele ment, type, handler) {
if (element.remove EventListener) {
element.removeE ventListener(ty pe, handler, false);
} else {
// delete the event handler from the hash table
if (element.events && element.events[type]) {
delete element.events[type][handler.$$guid];
}
}
};
function handleEvent(eve nt) {
var returnValue = true;
// grab the event object (IE uses a global event object)
event = event || fixEvent(((this .ownerDocument || this.document ||
this).parentWin dow || window).event);
// get a reference to the hash table of event handlers
var handlers = this.events[event.type];
// execute each event handler
for (var i in handlers) {
this.$$handleEv ent = handlers[i];
if (this.$$handleE vent(event) === false) {
returnValue = false;
}
}
return returnValue;
};
function fixEvent(event) {
// add W3C standard event methods
event.preventDe fault = fixEvent.preven tDefault;
event.stopPropa gation = fixEvent.stopPr opagation;
return event;
};
fixEvent.preven tDefault = function() {
this.returnValu e = false;
};
fixEvent.stopPr opagation = function() {
this.cancelBubb le = true;
};
</script>
<a href="index1.ht ml" id="onclick_hoi _index2.html">c lick me to
replace1</a><br/>
<a href="index2.ht ml" id="onclick_sal i_index.html_2" >click me to
replace2</a><br/>
<a href="index3.ht ml" id="onclick_sal i_index.html_3" >click me to
replace3</a><br/>
<a href="index4.ht ml" id="onclick_sal i_index.html_4" >click me to
replace4</a><br/>
<a href="index5.ht ml" id="onclick_sal i_index.html_5" >click me to
replace5</a><br/>