2

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
Michal Palko
  • 601
  • 1
  • 4
  • 14
  • Does this answer your question? [Open a URL in a new tab (and not a new window)](https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window) – jmargolisvt Nov 28 '20 at 18:32
  • they're just a few bugs.. like it works completely fine if you have the src as w3schools.. but just seeing which event to listen to to change it on its own(setTimeout before doing things in myWindow works but not very fast) – The Bomb Squad Nov 28 '20 at 18:33

1 Answers1

1

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')`)
}
The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17
  • This seems good but the way how I use it is that I add it as chrome bookmark with "javascript: " prefix - always works but for some reason not with your code. Am I doing something wrong? – Michal Palko Nov 28 '20 at 19:18
  • elaborate? I'm not sure I understand.. I mean i know about the `"javascript:codeHere;"` but what does that have to do with chrome bookmarks? – The Bomb Squad Nov 28 '20 at 19:19
  • I mean way I use it is that I save it as bookmark "javascript:code;" just because chrome has tendency to replace "javascript:" keyword when entering directly to url bar. I do not think it matters anyway. Basically my project is built that way (final product is only js code saved in bookmark (I do not have actual website). What I want to say is when I add "javascript:" to your code and try to execute in url bar it does not do anything. – Michal Palko Nov 28 '20 at 19:30
  • @MichalPalko try the javascript examples – The Bomb Squad Nov 28 '20 at 19:30
  • also i am still lost with what you mean.. ohhhhh you are talking about using a plain url and running ALL your code.. not really reliable though, you can make an html file and run the file instead?? – The Bomb Squad Nov 28 '20 at 19:37
  • Sorry for my bad explaining. Yes, that what I meant. I could do that but thats not objective of my project. Do you think saving like "https://cloudwebsite.com/mycode.js" and then execute in url could help? – Michal Palko Nov 28 '20 at 19:44
  • if cloudwebsite.com/mycode.js had any example of what i put, CERTAINLY.. just remember to have the `` layout – The Bomb Squad Nov 28 '20 at 19:47
  • I FOUND IT.. for urls, the special characters are formatted differently for a url to work.. i just pressed search on google to copy the formatted version of it.. javascript:window.open("..."%2C"MsgWindow"%2C"width%3D200%2Cheight%3D100").onload%3Dfunction()%7Bthis.document.write("xD")%7D – The Bomb Squad Nov 28 '20 at 20:35