1

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

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

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.com'>This is a link</a>

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

Deepak Kamat
  • 1,880
  • 4
  • 23
  • 38

2 Answers2

3

With jQuery:

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

Without jQuery:

document.addEventListener('DOMContentLoaded', function () {
  document.getElementById('dp99').click();
});
PitaJ
  • 12,969
  • 6
  • 36
  • 55
0

As seen on http://marcgrabanski.com/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;

}
James
  • 13,571
  • 6
  • 61
  • 83