get-url-parts
Get all parts of the current URL (Parse URL)
Installation
npm i get-url-parts
Example
// Import the getURL Object which contains all the methods; // For instance, the current URL in the browser is// https://www.example.com:3000/search?key-1=value-1&key-2=value-2#section-3 // Returns the whole current URL (string)// output: 'https://www.example.com:3000/search?key-1=value-1&key-2=value-2#section-3'getURLhref; // Returns the origin of the current URL (string)// output: 'https://www.example.com:3000'getURL; // Returns just the hostname without port (string)// output: 'www.example.com'getURLhost; // Returns the port of the current URL (string)// output: '3000'getURLport; // Returns the protocol of the current URL (string)// output: 'https:'getURLprotocol; // Returns the hash part of the current URL (string)// output: '#section-3'getURL; // Returns the path of the current URL (string)// output: '/search'getURL; // Returns the query string of the current URL (object)// output: { "key-1": "value-1", "key-2": "value-2" }getURL;
All the methods get the current URL from the address bar in browser, unless you give them your own (string) URL as their argument.
Example
// Put your own URL in a variableconst url = 'https://www.example.com:3000/search?key-1=value-1&key-2=value-2#section-3';// Pass url to the methods as their argument// and the output would be the same as the example abovegetURLhosturl;getURL;getURL;