Object Location in JavaScript. Server information, port numb... open

Object Location in JavaScript. Server information, port number, protocol. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

The Location Object belongs to the object Window and has the following properties:

  • href – full query string for the resource
  • pathname – resource path
  • origin – general query schema
  • protocol – protocol
  • port – the port used by the resource
  • host – host
  • hostname – hostname
  • hash – if the query string contains a pound sign (#), then this property returns the part of the string that comes after this character
  • search – if the query string contains a question mark (?), for example, then this property returns the part of the string that comes after the question mark

For example, we get the address of the current page:

    console.log(location.href);

The Location Object also provides a number of methods that can be used to manipulate by request:

  • assign(url) – loads a resource located at the path url
  • reload(forcedReload) – reloads the current web page. The forcedReload parameter specifies whether to use the browser cache. If the parameter is true, then the cache is not used
  • replace(url) – replaces the current web page with another resource located at the url path. Unlike the assign method, which also loads a web page from another resource, the replace method does not store the previous web page on the history stack, so we won’t be able to call the history.back() method to navigate to it.

To redirect to another resource, we can use both properties and methods of location:

location = "http://google.com";
location.href = "http://google.com";
location.assign("http://google.com");

To another local resource:

location.replace("index.html");
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation