I am working on some code to redefine onClick but have run into an issue and have no idea where to go from here. The issue arises when there is already something defined within the html for the onClick handler.
The following if statement would take a link, pull out the existing onclick info, then add the new info. There is a bunch of text stripping thats needs to be done in order to get rid of the IE and FF junk that is already showing up in the onClick.
The problem is, after I define everything the output ends up showing the actual variable rather than the contents of the variable. There must be some data type I could convert this to in order to get it working, but I really have no idea what it would be:
Output as of now
Code:
function(){ return existingClick }
I know that this should work, for the links that do not contain the existing onclick info I can just use the following (which does work):
Code:
document.links[i].onclick = function(){ return openQuickWindow(this.href);};
This is the code I am using:
Code:
if (typeof document.links[i].onclick == "function"){ // if 2
//define existing onclick and convert to string
var existingClick = String(document.links[i].onclick);
//define var for new function to be added
var newlink = "openQuickWindow(this.href);";
//remove the firefox and IE extras
existingClick = existingClick.replace("function","");
existingClick = existingClick.replace("onclick(event)","");
existingClick = existingClick.replace("{","");
existingClick = existingClick.replace("anonymous()","");
existingClick = existingClick.replace("}","");
//trim trailing spaces
existingClick = existingClick.replace(/^\s*/,"").replace(/\s*$/,"");
//make sure the existing onclick ends with a semicolon to avoid functions running together
if(existingClick.charAt(existingClick.length - 1) !== ";"){
existingClick = existingClick + ";";
}
// add new function to existing functions string
existingClick = existingClick + newlink;
//create new onclick event
document.links[i].onclick = function(){ return existingClick }
//debuging
alert(document.links[i].onclick);
}
|