我在刷新按钮或用户尝试退出重定向的页面时添加了重定向功能,但我想要发生的是当他们点击我的按钮时要卸载或不执行此功能
<a onClick="alert(you are going to tweeter);"href="http://twitter.com" class="tweetbutton" >I Like Twitter</a>
所以会发生什么是冲突和两个警报出现,我需要的是当点击按钮按钮点击onclick警报将出现然后被重定向到twitter.com而不执行以下功能
(function() {
setTimeout(function() {
var __redirect_to = 'http://facebook.com';
var _tags = ['btn', 'input'],
_go, _i, _i2;
for (_i in _tags) {
_els = document.getElementsByTagName(_tags[_i]);
for (_i2 in _go) {
if ((_tags[_i] == 'input' && _go[_i2].type != 'btn' && _go[_i2].type != 'submit' && _go[_i2].type != 'image') || _go[_i2].target == '_blank') continue;
_els[_i2].onclick = function() {
window.onbeforeunload = function() {};
}
}
}
window.onbeforeunload = function() {
setTimeout(function() {
window.onbeforeunload = function() {};
setTimeout(function() {
document.location.href = __redirect_to;
});
});
return 'you are leaving this page'
}
});
});
答案 0 :(得分:0)
按如下方式更新代码:
<a "href="http://twitter.com" class="tweetbutton" >I Like Twitter</a>
将此添加到您的js
document.addEventListener('click', function (e) {
if (e.target.className == 'tweetbutton') alert('you are going to twitter');
});