0

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

}())
Community
  • 1
  • 1
  • 4
    Possible duplicate of [Open a URL in a new tab (and not a new window) using JavaScript](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript) – roberrrt-s Oct 28 '16 at 08:45
  • Hello Roberrrt, That does not seem to help me. Could you please share the code if you know. – raja krishna Oct 28 '16 at 09:08

1 Answers1

0
function openInNewTab(pageUrl) {
var win = window.open(pageUrl, '_blank');
win.focus();
}
Jobin
  • 5,610
  • 5
  • 38
  • 53
  • Do you mind adding that over the code and sharing that here as complete one? I am a bit layman to this. Would be really grateful to you on this. – raja krishna Oct 28 '16 at 08:52
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – andreas Oct 29 '16 at 10:50