JavaScript: Get the website URL (loading page)
JavaScript Basic: Exercise-12 with Solution
Get Current Website URL
Write a JavaScript program to get the website URL (loading page).
The JavaScript program utilizes the document.URL property to retrieve the current website's URL. It then displays this URL in an alert dialog box, allowing users to see the URL of the page they are currently viewing. This property provides the URL of the document where the script is running.
Sample Solution:
JavaScript Code:
// Log the current website URL to the console
alert(document.URL);
Output:
https://s.codepen.io/boomerang/iFrameKey-2b7766e5-a65b-9c2a-459d-dabf749b1799/index.html
Explanation:
document.URL : The URL read-only property of the Document interface returns the document location as a string.
ES6 Version:
// Using ES6 template literals to log the current website URL to the console
console.log(`${document.URL}`);
Improve this sample solution and post your code through Disqus
Previous: JavaScript program to convert temperatures to and from celsius, fahrenheit.
Next: JavaScript exercise to create a variable using a user-defined name.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics