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
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
HTML Window createPopup() Method
Next article icon

HTML DOM Window alert() Method

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

The alert() method in HTML and JavaScript is used to display an alert box with a specified message. The alert box includes an OK button and is typically used to ensure that the user receives critical information or confirmation before proceeding.

Note: Alert boxes grab your attention and make you read a message and Using too many alerts stops you from doing other things on the webpage until you close them.

Syntax

alert(message);

Parameter:

Message: It is the message that is needed to show in the alert box and it's optional.

Example: Display an alert box by double-clicking a button. 

In this example the alert() is used to display a message box when the button is double-clicked. The message informs users about GeeksforGeeks. It's a simple way to show notifications or information.

html
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">     <title>Document</title> </head>  <body>     <h1 style="color: green;">         GeeksforGeeks     </h1>     <p>         For displaying the alert message, double         click the "Show Alert Message" button:     </p>      <button ondblclick="myalert()">         Show Alert Message     </button>      <script>         function myalert() {             alert("Welcome to GeeksforGeeks.\n " +                 "It is the best portal for computer" +                 "science enthusiasts!");         }     </script> </body>  </html> 

Output:

Example: Alert the hostname of the current URL

In this example we displays the hostname of the current URL when the "Show Hostname" button is clicked. The showHostname() function retrieves the hostname using window.location.hostname and shows it in an alert box.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">     <title>Alert Hostname Example</title> </head>  <body style="text-align: center;">     <h1 style="color: green;">         Geeks     </h1>     <p>         To see the hostname of the current URL,         click the "Show Hostname" button:     </p>      <button onclick="showHostname()">         Show Hostname     </button>      <script>         function showHostname() {             let currentURL = window.location.hostname;             alert("The hostname of the current URL is: " + currentURL);         }     </script> </body>  </html> 

Output:

jiu

Supported Browsers:

  • Google Chrome
  • Edge 
  • Firefox
  • Opera
  • Safari

Next Article
HTML Window createPopup() Method

S

Shubrodeep Banerjee
Improve
Article Tags :
  • Web Technologies
  • HTML
  • HTML-Methods

Similar Reads

  • HTML DOM Window atob( ) Method
    The Window atob() method is used for decoding a base-64 encoded string. It is used to decode a string of data which has been encoded using the btoa() method. It returns a string which represents the decoded string. Syntax : window.atob(EncodedString) Parameters Used : EncodedString : It is a mandato
    1 min read
  • HTML | DOM Window btoa() Method
    The Window btoa() method is used for encoding a string in base-64 format. The characters used by the Window btoa() method to encode the string are "A-Z", "a-z", "0-9", "+", "/" and "=" . Syntax: window.btoa(String) Parameters Used: String: It is a mandatory parameter which specifies the string to be
    1 min read
  • HTML DOM Dialog show() Method
    The DOM Dialog show() method is used to show the dialog. The Dialog element is accessed by getElementById(). It is used in HTML5. While using this method, the user can interact with other elements on the page. Syntax: dialogObject.show() Example: This example shows the working of Dialog show() Metho
    1 min read
  • HTML Window createPopup() Method
    The HTML createPopup() method was used to create custom popup windows in Internet Explorer but is now obsolete and not supported by modern browsers due to security concerns. Syntax: window.createPopup()Example: In this example we use the createPopup() method to generate a pop-up window with custom c
    2 min read
  • HTML DOM write() Method
    The write() method in HTML is used to write some content or JavaScript code in a Document. This method is mostly used for testing purposes. It is used to delete all the content from the HTML document and inserts the new content. It is also used to give additional text to an output that is opened by
    2 min read
  • HTML DOM close() Method
    The DOM close() method is used to close the output stream. It is used to indicate the finish of writing on a window. The close() method does not require any parameter.  Syntax: document.close()Example 1: In this example, we will use DOM close() method. [GFGTABS] HTML <!DOCTYPE html> <html
    2 min read
  • HTML DOM open() Method
    The DOM Open() method in HTML is the combination of the following three steps: Opens an output stream to collect the output.Collects output from the document.write() or document.writeln() methods.Runs the document.close to display the output written to the output stream. Syntax: document.open( MIMEt
    2 min read
  • HTML | DOM Window closed Property
    The Window closed property in HTML DOM is used to return a value that indicates whether the referred window is closed or not. Syntax: window.close() Return Value: A Boolean value, true if window is closed otherwise false. Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt
    1 min read
  • HTML DOM Dialog showModal() Method
    The DOM Dialog showModal() method is used to show the dialog. The Dialog element is accessed by getElementById(). It is used in HTML5. While using this method, the user can't interact with other elements on the page. To make the user interact with other elements, use the show() Method. Syntax: dialo
    1 min read
  • HTML DOM window document Property
    The HTML DOM window document returns a reference to the document contained in the current window. Syntax: doc = window.document; Return Value: This property returns a reference to the document. Example: In this example, we will get the title of the document using that document reference. C/C++ Code
    1 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