0

I have an HTML page which contains some clickable elements. I do not control the source of the page (think of my code as a browser extension). These elements are both buttons and divs. I would like to trigger the click programmatically when the user enters the page. Something like this:

function auto_click(some_div) {
    // do some other stuff
    some_div.click();
}

This function works, insofar as it executes a click event and successfully clicks the elements. However some of these elements are supposed to open a new page in another tab. While I can use the javascript DOM.click() function, the result is a redirection a browser redirection following a pop-up message within the current tab, rather than opening a new tab.

When I click the element on the page manually, it successfully opens the content in a new tab.

My Question:

Why can't I use the DOM.click() method to execute this in the same way as if I were clicking manually? Is there some other programmatic approach which would work?

Here is a fiddle illustrating my dilemma. I have commented out the auto-click code so you can view the difference yourself:

https://jsfiddle.net/7w1ngbsh/1/

clo_jur
  • 1,359
  • 1
  • 11
  • 27

1 Answers1

-1

The genuine way is to add _blank else you can check this answer

New tab issue

  • I don't think that link leads anywhere? And what am I adding `_blank` to? An argument into `click()`? That does not seem to be a viable argument. – clo_jur Aug 23 '21 at 17:44
  • Add target = '_blank' for opening it in new tab. – Mudassir Kidwai Aug 24 '21 at 09:25
  • Add `target = '_blank'` to what? The function does not accept any any arguments. Unless your suggestion is that I manipulate the DOM element before executing `click()`? If you take a look at the fiddle you'll notice that `_blank` is already included. The problem is that it doesn't work when executing programmatically. – clo_jur Aug 24 '21 at 16:12