I would like to make a button on a page that can call a JS function in the same page. The function will need to create [open] new window which its HTML code was given from the JS function itself. How can I do that?
The purpose of this is to produce a print friendly page out of a specific page.
Please notice: No AJAX can be used.
asked Jan 5, 2010 at 6:36
M. A. KishawyM. A. Kishawy
4,94711 gold badges46 silver badges72 bronze badges
var opened = window.open[""];
opened.document.write["MyTitletest"];
answered Jan 5, 2010 at 7:52
Fabien MénagerFabien Ménager
140k3 gold badges40 silver badges60 bronze badges
var w = window.open[""];
w.document.writeln[""]
answered Jan 5, 2010 at 6:39
Roy TangRoy Tang
5,4739 gold badges43 silver badges72 bronze badges
0
function fu[] {
var opened = window.open[""];
opened.document.write["Your HTML here"];
}
answered Jan 5, 2010 at 6:40
Emil VikströmEmil Vikström
88.5k15 gold badges134 silver badges170 bronze badges
3
Example 1
Open "www.w3schools.com" in a new browser tab:
window.open["//www.w3schools.com"];
Try it Yourself »
More examples below.
Definition and Usage
The open[]
method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.
Syntax
window.open[URL, name, specs, replace]
Parameters
Parameter | Description | |||||||||||||||||||||||
URL | Optional. The URL of the page to open. If no URL is specified, a new blank window/tab is opened | |||||||||||||||||||||||
name | Optional. The target attribute or the name of the window. The following values are supported: | |||||||||||||||||||||||
| ||||||||||||||||||||||||
specs | Optional. A comma-separated list of items, no whitespaces. The following values are supported: | |||||||||||||||||||||||
| ||||||||||||||||||||||||
replace | DeprecatedSpecifies whether the URL creates a new entry or replaces the current entry in the history list. The following values are supported:
WarningChrome throws an exception when using this parameter. Source: Bugs Chromium Issue 1164959. |
Return Value
A reference to the new window, or null if the call failed.
More Examples
Open an about:blank page in a new window/tab:
var myWindow = window.open["", "", "width=200,height=100"];
Try it Yourself »
Open a new window called "MsgWindow", and write some text into it:
var myWindow =
window.open["", "MsgWindow", "width=200,height=100"];
myWindow.document.write["
This is 'MsgWindow'. I am 200px wide and 100px tall!
"];Try it Yourself »
Replace the current window with a new window:
var myWindow = window.open["", "_self"];
myWindow.document.write["
I replaced the current window.
"];Try it Yourself »
Open a new window and control its appearance:
window.open["//www.w3schools.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400"];
Try it Yourself »
Open a new window. Use close[] to close the new window:
function openWin[] {
myWindow = window.open["", "myWindow", "width=200,height=100"]; // Opens a new window
}
function closeWin[] {
myWindow.close[]; // Closes the new window
}
Try it Yourself »
Open a new window. Use the name property to return the name of the new window:
var myWindow =
window.open["", "MsgWindow", "width=200,height=100"];
myWindow.document.write["
This window's name is: " + myWindow.name + "
"];Try it Yourself »
Using the opener property to return a reference to the window that created the new window:
var myWindow = window.open["", "myWindow", "width=200,height=100"]; // Opens a new window
myWindow.document.write["
This is 'myWindow'
"]; // Text in the new windowmyWindow.opener.document.write["
This is the source window!
"]; // Text in the window that created the new windowTry it Yourself »
Browser Support
open[]
is supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |