w3resource

JavaScript: Get the website URL (loading page)

JavaScript Basic: Exercise-12 with Solution

Write a JavaScript program to get the website URL (loading page).

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.



Follow us on Facebook and Twitter for latest update.