最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Execute Function Before A Link onClick Event - Stack Overflow

programmeradmin0浏览0评论

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 0
Add a ment  | 

2 Answers 2

Reset to default 5

First, 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>

发布评论

评论列表(0)

  1. 暂无评论