admin管理员组文章数量:1416631
I am performing DOM manipulation on a page where I don’t have access to the source code. I would simply like to prevent a link’s onClick handler from executing, add my own unrelated function then allow the original onClick function to execute as normal. Here is an example of the page code:
<a href="#" onclick="myAjaxFunction('param1, param2');return false;" name="buyLink" id="buyLink" >Buy Now</a>
I have e up with the code framework below:
jQuery('#buyLink').click(function(event){
event.preventDefault();
my2ndFunction(); // alert(‘I ran’);
//execute myAjaxFunction() here
});
I have two challenges:
1) When I use the Alert() code to test this out, the alert appears but the original function runs anyway (preventDefualt doesn’t seem to be working).
2) How do I call the original function plete with the correct dynamic parameter values? (maybe using “self” in some way?)
I am performing DOM manipulation on a page where I don’t have access to the source code. I would simply like to prevent a link’s onClick handler from executing, add my own unrelated function then allow the original onClick function to execute as normal. Here is an example of the page code:
<a href="#" onclick="myAjaxFunction('param1, param2');return false;" name="buyLink" id="buyLink" >Buy Now</a>
I have e up with the code framework below:
jQuery('#buyLink').click(function(event){
event.preventDefault();
my2ndFunction(); // alert(‘I ran’);
//execute myAjaxFunction() here
});
I have two challenges:
1) When I use the Alert() code to test this out, the alert appears but the original function runs anyway (preventDefualt doesn’t seem to be working).
2) How do I call the original function plete with the correct dynamic parameter values? (maybe using “self” in some way?)
Share Improve this question edited Aug 12, 2014 at 19:31 psoshmo 1,57010 silver badges19 bronze badges asked Aug 12, 2014 at 19:17 user1754738user1754738 3475 silver badges13 bronze badges 02 Answers
Reset to default 5First, back up the original onclick
handler. Then, remove it from the element. Finally, create your own handler. In your handler, call the original function.
var orig_onclick = $('#buyLink').prop('onclick');
$('#buyLink').removeProp('onclick');
$('#buyLink').click(function(e){
// Do your own code
my2ndFunction();
// Call the original function, with the correct context.
return orig_onclick.call(this, e.originalEvent);
});
DEMO: http://jsfiddle/qbz7wn9o/
Here is a way to achieve this without storing the onclick event.
var button = document.querySelector("button");
// add your listener
button.addEventListener("click", function(event) {
alert("listener action");
// calling stopImmediatePropagation here will prevent the inline action...
// ...from executing once it's moved to afterwards in the queue
//event.stopImmediatePropagation();
});
// move the inline listener to execute after yours
if (button.onclick !== null) {
button.addEventListener("click", button.onclick);
button.onclick = null;
}
<button onclick="alert('inline action');">click me</button>
本文标签: javascriptExecute Function Before A Link onClick EventStack Overflow
版权声明:本文标题:javascript - Execute Function Before A Link onClick Event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256383a2650127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论