Skip to main content

Remove hostname from URL in javascript

You can remove hostname from URL in javascript. Sometimes you have to remove the hostname from the URL in that case it will help you to remove the hostname (domain name) from the URL in javascript

  • Remove hostname from url in javascript using URL() constructor

    var url = new URL('https://w3codegenerator.com/queries/javascript')
    console.log(url.pathname)

    Output:

    /queries/javascript

    This code snippet return the url with pathname (without hostname) using URL() constructor method.

  • Back to code snippet queries related javascript