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

javascript - Auto click an anchor link on the page when the page is visited - Stack Overflow

programmeradmin7浏览0评论

I asked this question before but some of the experts told me to add

<meta http-equiv="refresh" content="2;url=/" />

That can reload a link given but i want to know how to click an element(anchor) with help of id. Is there any code that when executed will click on a id='dp99', and i want this javascript to be executed when the page is visited. Here's the HTML

<a id='d99' href=''>This is a link</a>

I will be grateful if anyone can help me !! Thank you.

I asked this question before but some of the experts told me to add

<meta http-equiv="refresh" content="2;url=http://www.example./" />

That can reload a link given but i want to know how to click an element(anchor) with help of id. Is there any code that when executed will click on a id='dp99', and i want this javascript to be executed when the page is visited. Here's the HTML

<a id='d99' href='http://someline.'>This is a link</a>

I will be grateful if anyone can help me !! Thank you.

Share Improve this question asked May 31, 2012 at 1:43 Deepak KamatDeepak Kamat 1,9805 gold badges24 silver badges41 bronze badges 3
  • Possible duplicate stackoverflow./questions/6157929/… – Nhu Trinh Commented May 31, 2012 at 2:07
  • Yup,, just like that.. Do you know how to do it ? – Deepak Kamat Commented May 31, 2012 at 14:39
  • If you start autoclicking ads on my browser my browser will stop visting your site :( – Mark Schultheiss Commented May 31, 2012 at 20:19
Add a ment  | 

2 Answers 2

Reset to default 4

With jQuery:

$(document).ready(function() { $('#dp99').click(); });

Without jQuery:

document.addEventListener('DOMContentLoaded', function () {
  document.getElementById('dp99').click();
});

As seen on http://marcgrabanski./articles/simulating-mouse-click-events-in-javascript

function mouseEvent(type, sx, sy, cx, cy) {

  var evt;

  var e = {

    bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0,

    screenX: sx, screenY: sy, clientX: cx, clientY: cy,

    ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,

    button: 0, relatedTarget: undefined

  };



  if (typeof( document.createEvent ) == "function") {

    evt = document.createEvent("MouseEvents");

    evt.initMouseEvent(type, e.bubbles, e.cancelable, e.view, e.detail,

    e.screenX, e.screenY, e.clientX, e.clientY,

    e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,

    e.button, document.body.parentNode);

  } else if (document.createEventObject) {

    evt = document.createEventObject();

    for (prop in e) {

      evt[prop] = e[prop];

    }

    evt.button = { 0:1, 1:4, 2:2 }[evt.button] || evt.button;

  }

  return evt;

}



function dispatchEvent (el, evt) {

  if (el.dispatchEvent) {

    el.dispatchEvent(evt);

  } else if (el.fireEvent) {

    el.fireEvent(‘on’ + type, evt);

  }

  return evt;

}
发布评论

评论列表(0)

  1. 暂无评论