JavaScript Open URL
In this section, you will learn how to open the url on the window.
As we know that window.location object that contains the current URL information. In the given code the document.forms[0].url.value gets the entered URL and used this object to open the entered URL. For this purpose, we have created a textbox to allow the user to enter any URL to open and click the button. As you click the button, the entered URL will get open.
Here is the code:
| <html> <script type="text/javascript"> function openURL(){ var urlToOpen = document.forms[0].url.value; window.location = urlToOpen; return false; } </script> <body bgcolor="lightBlue"> <form method="get" onsubmit="return openURL()"> Enter the URL to open: <input type="text" id="url"><br> <input type="submit" value="Submit"> </form> </body> </html> |
Output will be displayed as:
Enter any URL you want to open:

When you submit the button, the entered URL will get open:

Tutorials
- Java 26 (JDK 26) Features with examples
- Top 10 Reasons to Learn Java in 2026
- JDK 26 Tutorial
- Java Certification Roadmap 2026: Which Oracle Certs Matter
- Install JDK 26: A Complete Beginner's Guide (2026)
- What Is Java? A Complete, Up-to-Date Guide for 2026
- Java 26 HTTP3 tutorial - HTTP 3 support in JDK 26 - How to use HTTP/3 in Java 26 application?
- JDK 28 Preview: Value Objects, Shenandoah GC & JSON API
- Java Tutorials


