
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to get the title and full URL of a document in JavaScript?
In this article we will learn how to get the title and full URL of a document in JavaScript with the help of examples.
The Javascript HTML DOM has provided some basic methods to access and manipulate the HTML data. To find the title and URL of a document, JavaScript has provided document.title and document.URL respectively.
The document.title property − The document.title property returns the current title of the document. This property applies to HTML, SVG, XUL, and other documents. The syntax to get the title of the document is shown below.
document.title
The document.url property − The URL read-only property of the Document interface returns the full URL of the document. i.e., location of the document as a string, including the protocol (http://). It is similar to the Window Object property location.href which returns the URL of the current page. The syntax to get the full URL of the document is shown below.
document.URL or document.documentURI
Let us see some examples for these −
Example 1
Using the document.title property
The following is an example program on how to get the title of the document.
<!DOCTYPE HTML> <html> <head> <title>How to know the Title and URL of document in JavaScript</title> </head> <body style = "text-align:center;"> <h3>A sample program to know title of a document</h3> <p id="text1"></p> <script type="text/javascript"> document.getElementById("text1").innerHTML = "The Document's Title is : "+document.title; </script> </body> </html>
On executing the above code, the following output is generated.
Example 2
When we assign a text to document.title, the title of the document gets changed. This function is demonstrated in the following example.
<!DOCTYPE HTML> <html> <head> <title>How to know the Title and URL of document in JavaScript</title> </head> <body style = "text-align:center;"> <h3>A sample program to know title of a document</h3> <p id="text1"></p> <script type="text/javascript"> document.title = " New Title : A program to know the title of a document"; document.getElementById("text1").innerHTML = "The Document's New Title is : "+document.title; </script> </body> </html>
On executing the above code, the following output is generated.
Example 3
Using document.url property
The following is an example program on how to get the full URL of the document.
<!DOCTYPE HTML> <html> <head> <title>How to know the Title and URL of document in JavaScript</title> </head> <body style = "text-align:center;"> <h3>A sample program to know the FULL URL of a document</h3> <p id="text1"></p> <script type="text/javascript"> var url = location.href; var docURL = document.URL; document.getElementById("text1").innerHTML = "The Location of the Document is : "+docURL+"<br />"+"The document location by using Window object property : "+url; </script> </body> </html>
On executing the above code, the following output is generated.
- Related Articles
- How to show the full URL of a document with JavaScript?
- How to get the title and URL of a webpage with Javascript executor in Selenium with python?
- How to display the title of a document with JavaScript?
- How to get the title and URL of the page in Selenium with python?
- How to convert Title to URL Slug using JavaScript?
- How to get details of a webpage like url, title name, domain name etc using Javascript Executor in Selenium?
- Create HTML Document with Custom URL for the document in JavaScript
- How to get current URL in JavaScript?
- How to get the number of forms in a document with JavaScript?
- How to get the current URL with JavaScript?
- How to get the cookies associated with a document in JavaScript?
- How to get a particular anchor in a document in JavaScript?
- How to get the entire document HTML as a string in JavaScript?
- How to use the tag to define HTML document title?
- How to get an object containing parameters of current URL in JavaScript?
