JS Location USES detail

  • 2020-06-12 08:27:54
  • OfStack

The location address object in javascript describes the address opened by a window object. To represent the address of the current window, simply use "location"; To represent the address of a particular window, use the < Window object > location ".

1. The meaning of Location

1. The Location object is stored in the Location attribute of the Window object, which represents the Web address of the document currently displayed in that window. Its href attribute holds the entire URL of the document, while the other attributes describe individual parts of URL. These properties are very similar to the URL property of the Anchor object (or Area object). When an Location object is converted to a string, the value of the href attribute is returned. This means that you can use the expression location instead of location.href.

2. The Anchor object represents the hyperlink in the document, while the Location object represents the URL (or location) of the document currently displayed by the browser. But the Location object does much more than that. It also controls the location of the document displayed in the browser. If you give an Location object or its href attribute a string containing URL, the browser loads in and displays the document to which the new URL refers.

3. In addition to setting location or ES41en.href to replace the current URL with the full URL, you can also modify part of URL by assigning other attributes to the Location object. Doing so will create a new URL, one of which is different from the original URL and will be loaded and displayed by the browser. For example, if the hash property of the Location object is set, the browser moves to a specified location in the current document. Similarly, if the search attribute is set, the browser reloads URL with the new query string attached.

4. In addition to the URL attribute, the reload() method of the Location object can reload the current document, and replace() can load a new document without creating a new history for it, that is, in the browser's history list, the new document will replace the current document.

2. Location attribute in JS

Property description
hash sets or returns URL (anchor) starting with the well number (#). If there is no # in the address, an empty string is returned.
host sets or returns the hostname and the current PORT number of URL.
hostname sets or returns the hostname of the current URL.
href sets or returns the full URL. It is returned as it appears in the browser's address bar.
pathname sets or returns the path part of the current URL.
port sets or returns the current PORT number of URL, and sets or returns the current port number of URL.
protocol sets or returns the current URL protocol with the values 'http:','https:','file:', and so on.
search sets or returns from question mark (?) Start with URL (query section).

3. Location object methods in JS

Property description
assign() loads the new document.
reload() reloading the current document is equivalent to pressing the refresh (IE) or Reload (Netscape) key on your browser.
replace() replaces the current document with a new document, equivalent to pressing the refresh (IE) or Reload key on your browser.

4. Example of Location in JS


// Simple jump 

function gotoPage(url) { 

var url ="url?catalogid="+catalogID; 

window.location =url; 

}

// Pass parameters for a single page 

function goto_catalog(iCat) { 

if(iCat<=0) { 

top.location = "url";

} else { 

window.location ="url?catid="+iCat; 

}

}

//  Jumps to the specified frame 

function goto_iframe(url) { 

parent.mainFrame.location ="url"; 

}

This is the end of this article, I hope you enjoy it.


Related articles: