简体   繁体   English

在 chrome 中打开包含特定内容的新标签页

[英]Open new tab with specific content in chrome

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).我需要在浏览器中打开新选项卡,在其中为用户指定基本指令(可能是 html 字符串?)(我没有网站)。

Closest I got is this from w3schools :我从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)打开新的 window (我真的更喜欢新标签) - 这里的评论说取决于用户浏览器设置(所以也许解决了)
  2. does not work in Chrome (I tested 86.0.4240.198) - successfully tested in FF在 Chrome 中不起作用(我测试了 86.0.4240.198) - 在 FF 中成功测试

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问题是,您需要有一个来源,例如我的来源是chrome://newtab ,但是一旦您制作了"" ,它就不起作用了

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.请注意,这将打开chrome://newtab-page/...作为它的源,然后你用任何东西覆盖它。 In one last example, I will show you 2 ways of how to execute javascript from the new tab在最后一个示例中,我将向您展示如何从新选项卡执行 javascript 的 2 种方法

//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')`)
}

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

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