WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to Get Portion of URL Path in JavaScript

Topic: JavaScript / jQueryPrev|Next

Answer: Use the window.location Object

You can use the built-in window.location object to retrieve the different part or portion of URL path in JavaScript, as demonstrated in the following example:

// If URL is http://www.example.com/products/search.php?q=ipad#featured
 
 window.location.href // http://www.example.com/products/search.php?q=ipad#featured
  
 window.location.protocol // http:
  
 window.location.host // www.example.com (includes port if there is one e.g. 3000)
   
 window.location.hostname // www.example.com
  
 window.location.port // (provide port if there is one, otherwise empty string)
  
 window.location.pathname // /products/search.php
  
 window.location.search // ?q=ipad
  
 window.location.hash // #featured

Please check out the JavaScript window section to learn more about the window object.


Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties