In HTML, the anchor tag is used to open new windows and tabs in a very straightforward manner. However, there is a need to do the same using JavaScript. In JavaScript, window.open() proves to be helpful. The window.open() method is used to open a new browser window or a new tab depending on the browser setting and the parameter values.
Syntax:
window.open(URL, name, specs, replace);
Note: All the parameters are optional.
Approach:
- To open a URL in a new window, make sure that the second parameter is not _blank.
- The other parameters can be varied accordingly as per the need of the new window.
Example 1:
HTML
<!DOCTYPE html> < html > < body style = "text-align:center;" > < h1 style = "color:green" > neveropen </ h1 > < p > Click the button to open a new window. </ p > < button onclick = "NewTab()" > Open Geeksforneveropen </ button > < script > function NewTab() { window.open("https://www.geeksforgeeks.org", "", "width=300, height=300"); } </ script > </ body > </ html > |
Output:
- Before Clicking on the Button:
- After Clicking on the Button:
Example 2: Use Anchor tag to open URL in a new window.
HTML
<!DOCTYPE html> < html > < body style = "text-align:center;" > < h1 style = "color:green" > neveropen </ h1 > < p > Click the button to open neveropen in a new window. </ p > < a onclick= 'window.open("https://www.geeksforgeeks.org/", "_blank", " width = 300 , height = 300 ");'> neveropen </ a > </ body > </ html > |
Output:
- Before Clicking on neveropen:
- After Clicking on neveropen:
Example 3: Use the Input tag to open the URL in a new window.
HTML
<!DOCTYPE html> < html > < body style = "text-align:center;" > < h1 style = "color:green" > neveropen </ h1 > < p > Click the button to open neveropen in a new window. </ p > < input type = "button" onclick="window.open( 'https://www.geeksforgeeks.org/','neveropen', ' toolbars = 0 , width = 300 , height = 300 , left = 200 , top = 200 , scrollbars = 1 , resizable = 1 ');" value = "Open the window" > </ body > </ html > |
Output:
- Before Clicking on the Button:
- After Clicking on the Button:
Supported Browsers: The browsers are supported by the window.open() method are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.