简体   繁体   中英

Open link on a new tab on click of button

I wanted to add a button to a page which upon clicked should open a link on new tab of the same browser window. I have tried one of the codes that was available on Stack Overflow however that did not serve my purpose. I was able to position the button on the page and rename that as per my need. However Only open thing that I am looking forward to is upon clicking that button I would need a new tab to be opened with the link that I specify. Kindly share your inputs.

Code is the second one on this link: Add a JavaScript button using Greasemonkey or Tampermonkey?

(function(){
'use strict'

  window.addEventListener('load', () => {
  addButton('Create Case', selectReadFn)
  })

function addButton(text, onclick, cssObj) {
    cssObj = cssObj || {position: 'absolute', bottom: '4.2%', left:'24.3%', 'z-index': 3}
    let button = document.createElement('button'), btnStyle = button.style
    document.body.appendChild(button)
    button.innerHTML = text
    button.onclick = onclick
    Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key])
    return button
 }

function selectReadFn() {                     
[...document.getElementsByClassName('MN')].filter(isRead).forEach(element =>     element.click())
}

function isRead(element) {
    childs =  element.parentElement.parentElement.parentElement.getElementsByClassName('G3')
    return ![...childs].some(e => e.innerText.search(/unread/i)!==-1)
}

}())
function openInNewTab(pageUrl) {
var win = window.open(pageUrl, '_blank');
win.focus();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM