Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript BOM Location object

JavaScript BOM Location object

The Browser Object Model(BOM) provides the properties and methods for JavaScript to interact with the browser. BOM allows performing actions to manipulate the browser window through BOM objects without affecting the contents of the page i.e. the document. BOM objects are global objects. The BOM objects used to manipulate the browser window that is:

  • location
  • history
  • navigator
  • screen
  • document

These objects are children of the window object. The window object represents the browser window. So, they can be used either with the prefix: window.object_name or without using the prefix object_name

  • location.href returns the URL of the web page currently loaded in the browser window. 

Syntax:

console.log("URL of the web page " + location.href)
  • location.hostname returns the domain name of the current host (excluding the port number). 

Syntax:

console.log("Domain name of current host page is " + location.hostname)
  • location.protocol returns the web protocol being used by the current web page (http:, file: or https:) 

Syntax:

console.log("Protocol used by the current page is " + location.protocol)
  • location.assign returns a new web page loaded in the window, when the complete address is specified. 

Syntax:

location.assign("http://www.google.com")
  • location.reload reloads the current page. Its function is the same as that of reload button in the browser window. 

Syntax:

location.reload();

Example: This example uses location.href property of the location object. 

HTML




<!DOCTYPE html>
<html>
<body>
    <p id="a"></p>
   
    <script>
        document.getElementById("a").innerHTML
            = " URL of the currently loaded page is "
            + location.href;
    </script>
</body>
</html>


Output:

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments