简体   繁体   English

单击按钮,在新选项卡上打开链接

[英]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. 我尝试了Stack Overflow上提供的其中一种代码,但是没有达到我的目的。 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? 代码是此链接上的第二个代码: 使用Greasemonkey或Tampermonkey添加JavaScript按钮吗?

(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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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