
Skip to main content
Get hostname from URL in javascript
You can get hostname from URL in javascript. Sometimes you have to get the hostname from a specific URL. In that case this code snippet will help you to get the hostname from the url
-
Get hostname from URL in javascript using URL() constructor
const url = new URL('https://w3codegenerator.com/queries/javascript'); console.log(url.hostname);
Output:
w3codegenerator.com
You can get the hostname from any URL in javascript using URL constructor which returns the URL object. You can pass dynamic value instead of static value (https://w3codegenerator.com/queries/javascript) to URL() constructor.
-
Get hostname from current URL in javascript using location object
const hostname = window.location.hostname; console.log(hostname);
Output:
w3codegenerator.com
You can get the hostname from the current url in javascript using location object.
Bootstrap
Cmd
CodeIgniter
Css
Drupal
Git
Html
Javascript
Jquery
Laravel
Nodejs
Php
Python
Sql
Typescript
Wordpress
Random Code Snippet Queries: Javascript
- Generate 6 digit random number in javascript
- Deault order of record in datatable
- On click enter redirect to another URL with parameters in javascript
- Copy one array to another in javascript
- How to get query string value in javascript
- Execute function with condition in javascript
- Uncaught TypeError: Illegal invocation
- How to show and hide placeholder text
- How to check caps lock is on in javascript
- Trim the leading and trailing comma from string in Javascript
- Json stringify
- Concat variable with string in javascript
- How to get the length of a number in JavaScript
- How to crop multiple images with cropper js
- How to display an image in Javascript onclick
- How to check file is an image in javascript using filename
- Uncaught TypeError: Cannot read property 'addEventListener' of undefined
- How to preview a word document in HTML using javascript
- How to add a property to object with condition in javascript
- Remove property from object in javascript
- How to set href value of anchor tag in javascript
- Javascript to print Fibonacci series upto 15 Observations
- How to get file extension using javascript/jquery
- How to validate empty space in textbox in Javascript
- How to fixed bootstrap navbar at top on scroll