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(ajaxresponse).
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.toString());
}
function hoi(e){
alert('done' + this.params.toString());
}
//returns function for a function string
function getFunction(functionstr){
if(functionstr == 'sali'){
return sali;//function sali
}
else if(functionstr == 'hoi'){
return hoi;//function hoi
}
else{
return false;
}
//etc.
}
function init(){
traverseTags(document);
}
//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_FRAGMENT_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_NODE && n.attributes.length 0){
for(var i = 0; i < n.attributes.length; i++){
if(n.attributes[i].nodeName == 'id' &&
isEventTag(n.attributes[i].nodeValue)){// && n.hasAttributes() not iex
var call = new
callerObject(isEventTag(n.attributes[i].nodeValue));
var eve = call.getEvent();
var funct = call.getFunction();
var params = call.getParams();
addEvent(n, eve, getFunction(funct), 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(children[i]);
}
}
//checks if the id attribute is a event
function isEventTag(idAttribute){
var spliter = idAttribute.split('_');//spliter is the array with all
parameters form the id attribute: e.g.
onclick_somefunction_sometag_param1_param2_param3
if(arrEvents.contains(spliter[0])) return spliter;
return false;
}
//parses the array
function callerObject(callerArray){
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.length-2; i++){
paramArray[i] = callerArray[i+2];
}
this.getFunction = function(){
return funct;// Function(funct + '(' + paramArray.toString() +
');');
}
this.getParams = function(){
return paramArray;
}
this.getEvent = function(){
return getEventOfString(event);
}
function getEventOfString(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(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
// assign each event handler a unique ID
if (!handler.$$guid) handler.$$guid = addEvent.guid++;
// create a hash table of event types for the element
if (!element.events) 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(element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, 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(event) {
var returnValue = true;
// grab the event object (IE uses a global event object)
event = event || fixEvent(((this.ownerDocument || this.document ||
this).parentWindow || 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.$$handleEvent = handlers[i];
if (this.$$handleEvent(event) === false) {
returnValue = false;
}
}
return returnValue;
};
function fixEvent(event) {
// add W3C standard event methods
event.preventDefault = fixEvent.preventDefault;
event.stopPropagation = fixEvent.stopPropagation;
return event;
};
fixEvent.preventDefault = function() {
this.returnValue = false;
};
fixEvent.stopPropagation = function() {
this.cancelBubble = true;
};
</script>
<a href="index1.html" id="onclick_hoi_index2.html">click me to
replace1</a><br/>
<a href="index2.html" id="onclick_sali_index.html_2">click me to
replace2</a><br/>
<a href="index3.html" id="onclick_sali_index.html_3">click me to
replace3</a><br/>
<a href="index4.html" id="onclick_sali_index.html_4">click me to
replace4</a><br/>
<a href="index5.html" id="onclick_sali_index.html_5">click me to
replace5</a><br/>