Open new tab with specific content in chrome

Viewed 408

I need to open new tab in browser where I want to specify basic instructions (maybe html string?) for user (I don't have website for that).

Closest I got is this from w3schools:

var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");

However I have problems with that:

  1. opens new window (I really prefer new tab) - here in comments says depends on user browser settings (so maybe solved)
  2. does not work in Chrome (I tested 86.0.4240.198) - successfully tested in FF
1 Answers

Just open a site

//tab spawner
var myWindow=window.open("chrome://newtab");
myWindow.onload=function(){this.document.write('textHere')}

The thing is, YOU NEED TO HAVE A SOURCE, for example mine was chrome://newtab but once you make that "" it doesn't work

You can, however, use simple dots in your source to make it work. For example

//window spawner
var myWindow=window.open("...", "MsgWindow", "width=200,height=100");
myWindow.onload=function(){this.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>")}

Do note, this would open chrome://newtab-page/... as its source and then you overwrite it with whatever. In one last example, I will show you 2 ways of how to execute javascript from the new tab

//window spawner
var myWindow=window.open("javascript:alert('this message comes from url source')", "MsgWindow", "width=200,height=100");
myWindow.onload=function(){
  this.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>")
  this.window.eval(`alert('this message comes from the tab or window')`)
}
Related