Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Javascript Window prompt() Method
Next article icon

Javascript Window Open() & Window Close() Method

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Javascript Window.Open() method is used to open the web pages into a new window or a new tab. It depends on the browser settings and the values assigned to the parameter. 

Syntax:

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

Parameters: This method accepts four parameters as mentioned above and described below:

  • URL: It is an optional parameter. It is used to set the URL of web pages that need to open. If the URL is not set then window.open() method opens a blank window.
  • name: It is an optional parameter and is used to set the window name.
  • specs: It is an optional parameter used to separate the item using a comma.
  • replace: It is an optional parameter and used to specify the URL URL creates a new entry or replaces the current entry in the history list. This parameter returns a boolean value. If this parameter returns true then URL replaces the current document in the history list and if returns false then URL creates a new entry in the history list.

Return Value: This method creates a new window. 

Window.close(): This method is used to close the window which is opened by the window.open() method. 

Syntax:

window.close()

Parameters: This method does not contain any parameters. 

Return Value: This method does not return any value. 

Example: The below example illustrates the window.open() and window.close() method in Javascript. If you click on the Open GeeksforGeeks button then the geeksforgeeks.org page opens in a new window and if you click on the close GeeksforGeeks button then the geeksforgeeks.org windows will close.

HTML
<!DOCTYPE html> <html>  <head>     <title>window open and close method</title> </head>  <body style="display: flex;              flex-direction: column;             justify-content:center;             align-items:center;">     <div class="container" style="position: relative;                 text-align: center;">         <h1 style="color: rgb(18, 154, 18);">             GeeksforGeeks         </h1>         <h3> window open and close method</h3>         <button onclick="windowOpen()">             Open GeeksforGeeks         </button>         <button onclick="windowClose()">             Close GeeksforGeeks         </button>     </div>     <script>         let Window;          // Function that open the new Window         function windowOpen() {             Window = window.open(                 "https://www.geeksforgeeks.org/",                 "_blank", "width=400, height=300, top=230, left=540");         }          // function that Closes the open Window         function windowClose() {             Window.close();         }     </script> </body>  </html> 

Output: 

https://media.geeksforgeeks.org/wp-content/uploads/20240801095227/Untitled30.mp4

Supported Browser: The browsers are supported by the window.open() & window.close() method are listed below:

  • Chrome version 1 and above
  • Edge version 12 and above
  • Firefox version 1 and above
  • Internet Explorer version 4 and above
  • Opera  version 3 and above
  • Safari version 1 and above

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.

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.



Next Article
Javascript Window prompt() Method

N

Naman_Garg
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Methods

Similar Reads

  • Javascript Window Blur() and Window Focus() Method
    JavaScript provides various methods to manipulate browser windows, two of which are window.blur() and window.focus(). These methods are particularly useful when managing the user interface and enhancing the user experience by controlling window focus. Javascript Window Blur()Javascript Window Blur()
    3 min read
  • JavaScript window.close() Method
    JavaScript window.close() method, is used for closing a certain window or tab of the browser which was previously opened by the window.open() method. Syntax: window.close();Parameters: This method does not accept any parameters. Example: In this example, at first we will use the window.open() method
    2 min read
  • JavaScript window.open() Method
    The Javascript window.open() method is used to open a new tab or window with the specified URL and name. It supports various parameters that can be used to specify the type and URL location of the window to be opened. Syntax: window.open(url, windowName, windowFeatures)Parameters: It has the followi
    3 min read
  • Javascript Window prompt() Method
    The prompt() method in JavaScript displays a dialog box that prompts the user for input. The prompt() method returns the input value when the user clicks "OK" else returns null. Syntax:prompt(message, default);message is a string of text to display to the user. It can be omitted if there is nothing
    2 min read
  • Javascript Window confirm() Method
    The confirm() method in JavaScript displays a dialog box with a message and two buttons: OK and Cancel. It is often used to get user confirmation before an action, returning true if OK is clicked, and false if Cancel is clicked. Syntaxconfirm(message);Parametersmessage: It is the optional string to
    3 min read
  • How to open URL in a new window using JavaScript?
    In HTML, the anchor tag (<a>) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open() method becomes useful. The window.open() method is used to open
    3 min read
  • How to Open a Popup on Click using JavaScript ?
    Opening a Pop-up when the click action is performed is the user-interactive technique. We can use this functionality in messages, or forms to show some error or alert messages before the user moves to the next action. Table of Content Using display propertyUsing classList PropertyUsing visibility pr
    5 min read
  • How to close current tab in a browser window using JavaScript?
    In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo make this, we will use window.close() method.  This method is used to close the window which is opened by the window.open() method.  Syntax:window.close();But accounting for a secu
    1 min read
  • How to close window using JavaScript which is opened by the user with a URL ?
    JavaScript does not allow one to close a window opened by the user, using the window.close() method due to security issues. However, we can close a window by using a workaround. The approach to be followed is by opening the current URL using JavaScript so that it could be closed with a script. Synta
    2 min read
  • JavaScript | window.location and document.location Objects
    window.location and document.location: These objects are used for getting the URL (the current or present page address) and avert browser to a new page or window. The main difference between both is their compatibility with the browsers. The window.location is read/write on all compliant browsers. T
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences