Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My script clicks an image on a site. The image has an anchor href and an onclick href, but the onclick href has a confirm box that pops up once it's clicked.
The onclick HTML is:

onClick="this.href='link2';if (!confirm('Are you sure?')) { return false; }

How do I get the script to click OK in that confirm box, once it pops up?

I'm using this function to click the picture link:

function click(elm) {
 var evt = document.createEvent('MouseEvents');
 evt.initMouseEvent('click', true, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null);
 elm.dispatchEvent(evt);
}
click(picture element)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
812 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can override the confirm method to temporarily silence it and return true. Below example will silence a confirm dialog only once, and only if it's called. Once the silenced dialog is executed, it will restore its original functionality and subsequent call to confirm method will work as normal.

var realConfirm=window.confirm;
window.confirm=function(){
  window.confirm=realConfirm;
  return true;
};
click(picture element);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...