0

I'm working on a javascript macro to open a specific link on a site in a new tab, but I'm not able to get the link to open in a new tab.

Here's the code:

document.onload = window.frames[0].document.getElementsByClassName('TextCell SA Link FW100 AL')[0].click()

I've tried using the window.open() function, but it doesn't seem to be working for me. Here's how I wrote it

document.onload=window.open(window.frames[0].document.getElementsByClassName('TextCell SA Link FW100 AL')[0].click(),'_blank')

HTML:

<td class="TextCell SA Link FW100 AL" 
    onclick="(function(e) { e.preventDefault(); e.stopPropagation(); return autotask.siteNavigation.__openPage(new Autotask.PopupPage('ProjectDetail','\/projects\/views\/100\/prjView_100.asp',false,'objectID','2359',false,false,new Autotask.PopupSettings(550,null,950,null,false))); })(event)">
    P20190930.0003
</td>
naveen
  • 53,448
  • 46
  • 161
  • 251
Tom D
  • 351
  • 1
  • 4
  • 13

1 Answers1

0

Syntax of window.open is

var window = window.open(url, windowName, [windowFeatures]);

Here, as you are trying to grab the click event of the td, window.open wont work

Try this.

// document.addEventListener('load', (event) =>
document.onload = (event) => {
  // var tdList = window.frames[0].document.getElementsByClassName('TextCell SA Link FW100 AL');
  var tdList = window.frames[0].document.querySelectorAll('.TextCell.SA.Link.FW100.AL');
  if(tdList && tdList.length) {
    tdList[0].click();
  } else {
    alert('TD not found.');
  }
};

Please note that it

naveen
  • 53,448
  • 46
  • 161
  • 251
  • Gave this a shot, but it's not clicking the link or displaying an alert – Tom D Dec 21 '19 at 23:41
  • So, the `HTML ELement` is *found*. You should alert (tdList[0].inner.HTML) and see if its the same element we are checking. There will be multiple tds within multiple trs – naveen Dec 21 '19 at 23:50
  • I cant even get that to work. Here's the code im using `document.onload = (event) => { // var tdList = window.frames[0].document.getElementsByClassName('TextCell SA Link FW100 AL'); var tdList = window.frames[0].document.querySelectorAll('.TextCell.SA.Link.FW100.AL'); if(tdList && tdList.length) { alert(tdList[0].innerHTML); } else { alert('TD not found.'); } }; ` – Tom D Dec 22 '19 at 00:28
  • I took out the `document.onload = (event)` and just ran the `var tdList = window.frames[0].document.querySelectorAll('.TextCell.SA.Link.FW100.AL');alert(tdlist[0].innerHTML) ` and the alert showed the correct link – Tom D Dec 22 '19 at 00:37
  • if I remove the `document.onload = (event) =>` it does, but it still opens in a new window – Tom D Dec 22 '19 at 00:45
  • window.open always opens a new window. AFAIK, I dont know of a way to make it always open in a new tab https://stackoverflow.com/q/4907843/17447 – naveen Dec 22 '19 at 00:48