简体   繁体   中英

How to open a new browser tab with no URL (via onClick on webpage) as if you clicked the “+” button to add a new tab

How does one go about opening a new tab with no URL in the address bar via an onClick event from a webpage as if you clicked on the "+" button to the right of all the tabs.

Yes, I can do onClick={() => window.open('', '_blank')} to open a new tab without a URL. But that will result in about:blank appearing in the address bar. Something I don't want. The new tab that opens should be exactly as if you clicked on the "+" button .

JFYI, I'll also be okay if instead of a new tab opening, the page where the onClick originated from becomes the blank page. As if you clicked the home button.

Unfortunately there isn't a way to do what you want using available browser APIs. The syntax of window.open is window.open(URL, name, specs, replace) . The first 2 is what defines how this new "window" is opened. The url of where the window should go. This can be left as an empty string, but will open a window as "about:blank". The 2nd part is the name, which defines how the window should be open (tab, window, etc).

Local resources can't be loaded with this URL, so "chrome://newtab" won't work. Trying to use a period as your url will open the new window with whatever page the window was opened from. Trying to use a slash as the url will open the new window as the base path of where the new window was created from. Invalid urls that I tried (random strings and characters) will just open a new window with your current url, but also append whatever you typed.

From what it looks like, you're stuck with "about:blank", or having to look in to other options.

Sources for reference

about:newtab seems to work in chrome and Firefox but not in edge. While about:home works in Firefox and edge but not chrome. You could probably detect the web browser and act accordingly.

Update:

Sorry, these pages appear to be privileged and cannot be acceded by other websites.

could you use any of these

<a href="javascript:if(navigator.appName.indexOf('Microsoft Internet Explorer') != -1){location='http://facebook.com/';}if(navigator.appName.indexOf('Netscape') != -1){
window.open('http://facebook.com/','','width=600,height=400,left=50,top=50,toolbar=yes');};void 0">
New new window</a>

<input type="button" value="
New new window" onclick="javascript:window.open('http://facebook.com/','','width=600,height=400,left=50,top=50,toolbar=yes');" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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