• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
18 views
I want to open a new tab/window and do some writing of custom HTML on it?

I tried following with fails:

myWindow=window.open('','','width=500,height=200')

myWindow.document.write("<p>Hello 'World'</p>")

myWindow.focus()

I also tried:

window.open('<html><head></head><body>'Hello World'</body></html>');

Can anyone show me what wrong i am doing?
by (930 points)  

2 Answers

0 votes

How to Open URL in New Tab using JavaScript with Content

javascript open new tab with content

We have been using <a> tag of HTML to open a new link URL but there might be need to open a new tab with content using Javascript. How do we do this?

Using window.open() method we can open a URL in a new tab.

window.open('https://www.kodlogs.com', '_blank');

by (930 points)  
edited by
0 votes

More Example to open a new tab with content or without content in Javascript:

Example

Open "Kodlogs.com" in a new browser tab:

window.open("https://www.Kodlogs.com");

Definition of open

This method opens browser in new window or a new tab based on your browser capabilities set up.

Browser Supported

IE, Chrome, Safari, Firefox

Syntax for open

window.open(URL, name, specs, replace)

Example1

var myWindow = window.open("", "", "width=100,height=50");

Example2

Showingopening a new window and writing content on it

var win = window.open("", "MsgWin", "width=100,height=50");

myWindow.document.write("<p>Hello World</p>");

Example3

Replacing exisitng window with a new window:

var win = window.open("", "_self");

myWindow.document.write("<p>Replacing exisitng window.</p>");

Example4

Appearance of new window:

window.open("https://www.kodlogs.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=400,left=400,width=300,height=300");

Example5

multiple tabs opening windows with:

window.open("http://www.kodlogs.com/", "width=);

window.open("https://www.stackoverflow.com/");

Example6

Example of close() for closing the window:

function openWin() {

  win = window.open("", "win", "width=200,height=100");   // opening new window

}

function closeWin() {

  win.close();   // Closing the new window

}

Example7

Using the name property to return the name of the new window:

var win = window.open("", "Msgwin", "width=100,height=80");

myWindow.document.write("<p>Hello World"</p>");

by (930 points)  
...