Open new window with javascript simple tutorial
Published by Aslam
In this tutorial, you are going to learn how to open a new window with javascript.
It will be useful if you decide to show preview of something,or to open on a buttonclick.
Basic code looks likes this ->
Optional Attributes are
You can specify width of the new window. it can be any.
You can specify height of the new window. it can be any.
This resizable attribute can be used to control whether you want the window can be resized or not.
This attribute can be used to show or hide scrollbars on the new window.
It can be used to show or hide toolbar in the new window, toolbar is where your browser's back,forward,reload buttons appears,
To show or hide location,url box,or where the address of page appears.
To show or hide extra tools on ur browsers toolbar.
To show or hide status bar at the bottom of th window .
To show or hide menubar of your browser. file ,edit, etc.
To copy history of old window to new window, or not.
Check this live example
For an empty window (not for tab), please use _blank
If you want to do it with a function,
Implimentation of this function Example
screenX number in pixels, the position of window from top. this code is for netscape browsers.
screenY number in pixels, the position of window from left. this code is for netscape browsers.
top number in pixels, the position of window from top. this code is for IE 4+. 4.
left number in pixels, the position of window from left. this code is for IE 4+. 4.
Example Code
To close an opened window, use window.close();
Example code
It will be useful if you decide to show preview of something,or to open on a buttonclick.
Basic code looks likes this ->
window.open(URL,name,Attributes) ;
Where URL is your specified URL to open, name is the title or name for the tab,attributes are the optional attributes we can use.Optional Attributes are
- width = 300
You can specify width of the new window. it can be any.
- height = 400
You can specify height of the new window. it can be any.
- resizable = yes or no / 0 or 1
This resizable attribute can be used to control whether you want the window can be resized or not.
- scrollbars = yes or no / 0 or 1
This attribute can be used to show or hide scrollbars on the new window.
- toolbar = yes or no / 0 or 1
It can be used to show or hide toolbar in the new window, toolbar is where your browser's back,forward,reload buttons appears,
- location = yes or no / 0 or 1
To show or hide location,url box,or where the address of page appears.
- directories = yes or no / 0 or 1
To show or hide extra tools on ur browsers toolbar.
- status = yes or no / 0 or 1
To show or hide status bar at the bottom of th window .
- menubar = yes or no / 0 or 1
To show or hide menubar of your browser. file ,edit, etc.
- copyhistory = yes or no / 0 or 1
To copy history of old window to new window, or not.
<form>
<input type="button" value="New Window!" onClick="window.open('http://www.google.com','google','width=640,height=460')"/>
</form>
Check this live example
For an empty window (not for tab), please use _blank
window.open('', '_blank', 'toolbar=0,location=0,menubar=0');
If you want to do it with a function,
<script type="text/javascript">
function open_new_window(URL)
{
NewWindow=window.open(URL,"_blank","toolbar=no,menubar=0,status=0,copyhistory=0,scrollbars=yes,resizable=1,location=0,Width=1500,Height=760") ;
NewWindow.location = URL;
}
</script>
Implimentation of this function Example
<a href="#" onClick="open_new_window('http://www.google.com');">google search</a>
<a href="#" onClick="open_new_window('http://www.yahoo.com');">yahoo search</a>
Set position of new windowscreenX number in pixels, the position of window from top. this code is for netscape browsers.
screenY number in pixels, the position of window from left. this code is for netscape browsers.
top number in pixels, the position of window from top. this code is for IE 4+. 4.
left number in pixels, the position of window from left. this code is for IE 4+. 4.
Example Code
<form>
<input type="button" value="New Window!" onClick="window.open('http://google.com','google search','width=700,height=600,left=0,top=100,screenX=0,screenY=100')">
</form>
To close an opened window, use window.close();
Example code
<form>
<input type="button" value="Close Window" onClick="window.close()">
</form>
Buen Tutorial, es el único que he encontrado que funciona!
ReplyDeleteThanks!