javascript - Replacing an onclick event on a hyperlink with a slightly altered function -



javascript - Replacing an onclick event on a hyperlink with a slightly altered function -

my problem @ moment have email hyperlink on page opens new email pop-up screen. each time email link clicked, to: field in email pop populated email address attached hyperlink. if email has apostrophe in it, first time clicked work ok, each time after alter apostrophe '%27' (as seen in image below).

the thought in script here set of links in page array, search through array 1 class of "emaillink" , replace onclick event on link function of own, removes '%27' in link , puts in apostrophe.

<script> // when page loads $(document).ready(function () { // load of link elements array var alllinks = document.getelementsbytagname('a'); // iterate through of links on page (var i=0; i<alllinks.length; i++) { // when find 1 class of "emaillink" (an email link) if (alllinks[i].classname=="emaillink") { // load text of onclick event auto var stroldonclick = alllinks[i].onclick + ''; // create regex matches single quote, along part of function // used launch email pop-up var testregex = /(mailto\(\'.*\%27.*\',event,.*\))/gi; // check if link contains encoded single quote if (testregex.test(stroldonclick)) { // execute regex against onclick event // (loads part of variable 1 (regexp.$1)) testregex.exec(stroldonclick); // replace right onclick text in string var var newfunc = "alert('newfunc running'); if(parent&&parent.frames['eware_menu'])fn=parent;" + "else fn=opener.parent;" + "if(fn&&fn.frames['eware_menu'])" + "fn.frames['eware_menu']." + regexp.$1.replace('%27', "\\'") +";" // replace onclick event link updated value $("before test: " + [alllinks[i]]).attr('onclick','').unbind('click'); $([alllinks[i]]).bind("click", function(newfunc)); } } } }); </script>

this original onclick event on link when view source of page:

if(parent&&parent.frames['eware_menu'])fn=parent;else fn=opener.parent;if(fn&&fn.frames['eware_menu'])fn.frames['eware_menu'].mailto('r%27sakai@demo.com',event,'&key0=3&key1=116&key2=169');

note: know onclick getting changed correctly alert coming when click on link, way have changed javascript in (var newfunc)

a variation of works in net explorer not in chrome (or firefox) , going used.

you not need write function of own, utilize javascript unescape() function remove apostrophe. if mailto contains string apostrophe:

var readablestring = unescape(mailto);

javascript jquery regex onclick

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -