【发布时间】:2014-06-19 13:15:01
【问题描述】:
我目前的问题是我在打开新电子邮件弹出屏幕的页面上有一个电子邮件超链接。每次单击电子邮件链接时,电子邮件弹出窗口中的收件人:字段将由附加到超链接的电子邮件地址填充。如果该电子邮件中有一个撇号,第一次点击它会正常工作,但每次它都会更改“%27”的撇号(如下图所示)。
我的脚本中的想法是将页面中的所有链接放入一个数组中,在该数组中搜索具有“EMAILLINK”类的链接,并将该链接上的 onclick 事件替换为我的函数own,它会删除链接中的“%27”并添加一个撇号。
<script>
// When the page loads
$(document).ready(function () {
// Load all of the link elements into an array
var allLinks = document.getElementsByTagName('a');
// Iterate through all of the links on the page
for (var i=0; i<allLinks.length; i++) {
// When you find one with a class of "EMAILLINK" (an email link)
if (allLinks[i].className=="EMAILLINK") {
// Load the text of the onclick event into a car
var strOldOnclick = allLinks[i].onclick + '';
// Create a regex that matches a single quote, along with part of the function
// used to launch the email pop-up
var testRegex = /(MailTo\(\'.*\%27.*\',event,.*\))/gi;
// Check if the link contains an encoded single quote
if (testRegex.test(strOldOnclick)) {
// Execute the regex against the onclick event
// (loads part of it into variable 1 (RegExp.$1))
testRegex.exec(strOldOnclick);
// Replace the correct onclick text in your 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 the onclick event for the link with the updated value
$("Before test: " + [allLinks[i]]).attr('onclick','').unbind('click');
$([allLinks[i]]).bind("click", Function(newFunc));
}
}
}
});
</script>
这是我查看页面源码时链接上的原始onclick事件:
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');
注意:我知道 onclick 会正确更改,因为当我单击链接时会出现警报,所以这与我在 (var newfunc) 中更改 Javascript 的方式有关
它的一个变体在 Internet Explorer 中有效,但在 Chrome(或 Firefox)中无效,这就是它将被使用的地方。
【问题讨论】:
-
使用 jquery,您只需使用 'EMAILLINK' 类抓取链接,无需抓取页面上的所有链接。你在问什么?有什么问题?你能澄清一下吗
-
你也可以用普通的 Javascript 来做到这一点,使用
document.getElementByClassName。 -
为什么不直接使用
decodeURIComponent而不是直接替换%27? -
@Barmar - 把它作为答案,这就是他们需要做的所有事情,然后重新格式化所有这些代码
-
@ScottSelby 我不确定这会解决 Chrome/Firefox 和 IE 之间的区别。他需要弄清楚为什么IE中有编码地址,而Chrome/FF没有。
标签: javascript jquery regex onclick