Open In App

HTML DOM Location assign( ) Method

Last Updated : 14 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Location assign() method is used for loading a new document. It can also be used for loading a new document but the difference between these two methods is that the replace() method removes the URL of the current document from the document history and therefore it is not possible to navigate back to the original document using the “back” button. 

Syntax:

location.assign(URL)

Parameters Used:

  1. URL: It is a mandatory parameter which specifies the URL of the page to navigate to.

Below program illustrates the Location assign() Method : 

Loading a new document. 

html




<!DOCTYPE html>
<html>
  
<head>
    <title>Location assign() Method in HTML</title>
    <style>
        h1 {
            color: green;
        }
          
        h2 {
            font-family: Impact;
        }
          
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
  
    <h1>GeeksforGeeks</h1>
    <h2>Location assign() Method</h2>
  
    <p>For loading a new document, 
      double click the "Load Document" button: </p>
  
    <button ondblclick="load()">Load Document</button>
  
    <script>
        function load() {
            location.assign("https://www.geeksforgeeks.org");
        }
    </script>
  
</body>
  
</html>
                      


Output:

  

After clicking the button

  

Note: Above program would not work for all links as cross framing is not allowed in many cases, try it by assigning link “https://ide.geeksforgeeks.org/”. 

Supported Web Browsers

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 5.5
  • Firefox 1
  • Opera 3
  • Safari 3


Similar Reads

Difference between window.location.href, window.location.replace and window.location.assign in JavaScript
Window.location is a property that returns a Location object with information about the document's current location. This Location object represents the location (URL) of the object it is linked to i.e. holds all the information about the current document location (host, href, port, protocol, etc.) All three commands are used to redirect the page t
4 min read
JavaScript | window.location and document.location Objects
window.location and document.location: These objects are used for getting the URL (the current or present page address) and avert browser to a new page or window. The main difference between both is their compatibility with the browsers. The window.location is read/write on all compliant browsers. The document.location is read-only in Internet Expl
2 min read
How to set location and location.href using JavaScript ?
In this article, we will set the location and location.href using Javascript. Both location and location.href are used to set or return the complete URL of your current page. They return a string that contains the entire URL with the protocol. Syntax: location = "https://www.geeksforgeeks.org"; or location.href = "https://www.geeksforgeeks.org"; Bo
2 min read
JavaScript location.host vs location.hostname
In this article, we will learn about how to get information related to the webpage like hostname, port number, etc. We will also see the difference between the location.hostname and location.host. location object is used to get information about the current web page. host and hostname are properties of location objects. location.hostname: This prop
2 min read
HTML | DOM Location replace() Method
The DOM Location replace() Method in HTML is used to replace the current document with the specified one. This method is different from the assign() method as this removes the current document from the document history, therefore it is not possible to go back to the previous document using the 'back' button. Syntax: location.replace( newUrl ); Para
1 min read
HTML DOM Location reload() Method
The location reload() method in HTML DOM is used to reload the current document. This method refreshes the current documents. It is similar to the refresh button in the browser.  Note: It does not return any value.  Syntax:location.reload( forceGet )Parameters: It does not take any parameters Example: This example illustrates the use of the locatio
1 min read
HTML DOM Location hostname Property
The Location hostname property in HTML is used to return the hostname of the current URL. The Location hostname property returns a string that contains the domain name, or the IP address of a URL. Syntax: It returns the hostname property. location.hostname It is used to set the hostname property. location.hostname = hostname The below program illus
1 min read
HTML DOM Location host Property
The Location Host property in HTML is used to sets or return the hostname and port of a URL. The Location Hash property doesn't return the port number if it is not specified in the URL. Syntax: It returns the host property. location.hostIt is used to set the host property. location.host = hostname:portProperty Value: hostname:port: It contains a st
1 min read
HTML DOM Location hash Property
The Location Hash property in HTML is used to return the anchor part of a URL. It can also be used to set the anchor part of the URL. It returns the string which represents the anchor part of a URL including the hash '#' sign. Syntax: It returns the hash property.location.hashIt is used to set the hash property. location.hash = anchorname Property
2 min read
HTML DOM Location pathname Property
The Location pathname property in HTML is used to set or return the pathname of a URL. The Location pathname property returns a string that represents the pathname of the URL. Syntax: It returns the pathname property.location.pathnameIt is used to set the pathname property.location.pathname = pathExample: Below program illustrates the Location path
1 min read