Archive Pages Design$type=blogging

Various parts of URL using JavaScript

Various parts of URL using JavaScript

Recently I wanted to get a domain name of the page on which I was currently on. I was trying to parse the entire URL of the page and then parse it using regular expressions etc. to get the domain name. I was able to get the required information using this method, but then I thought that this is something for which there should be an easier way, and indeed there is an easier way.
We can use the Location Object to get all the various parts of the URL. (e.g. window.location.href to get the entire URL)
Note: There is no public standard that applies to the location object, but all major browsers support it.



A URL may consists of as many as 6 parts. My previous blog post “Parts of URL” explains the different parts of a URL.
Now, we will see how we can get all these different parts of the URL using the Location object. We can use the different properties of the Location object to get all the parts.
PropertyDescription
hashReturns anchor portion of the URL, including the leading hash. (“#results”)
hostReturns the hostname and port (if available) of a URL. (“www.example.com” or “www.example.com:80”)
hostnameReturns the hostname portion of the URL (“www.example.com”)
hrefReturns the entire URL. (“http://www.example.com:80/example.php?x=1&y=2#results”)
pathnameReturns the path name of the URL (“/example.php”)
portReturns the port portion of the URL (“80” within the host “www.example.com:80”)
protocolReturns the protocol portion of the URL, including the trailing colon (“http:”, “https:”, “ftp:”, etc)
searchReturns the query portion of the URL, including the question mark (“?x=1&y=2”)

COMMENTS

Name

adobe ai Amikom android anime app audiojungle automation background blizzard blogger business card cdr code coding Creative css demon slayer desain logo design entertainment envato eternals exariva.com font free freebie freebie & free file game gaming gaya hidup giveaway graphic design hardware hearthstone html idea illustrator imacros inspiration internet japan javascript komputer kreativemagz Logo logo vector marvel mcu mockup money movie nvidia pc pdf pelajaran hidup Photography problem solving programming scss self improvement software steam stock photo tampermonkey Template theme tips trailer trick tutorial typeface typography userscript vector video virtual link king wallpaper windows youtube
false
ltr
item
kreativeMAGZ: Various parts of URL using JavaScript
Various parts of URL using JavaScript
Various parts of URL using JavaScript
kreativeMAGZ
https://kreativemagz.blogspot.com/2019/03/various-parts-of-url-using-javascript.html
https://kreativemagz.blogspot.com/
https://kreativemagz.blogspot.com/
https://kreativemagz.blogspot.com/2019/03/various-parts-of-url-using-javascript.html
true
2977666615667296227
UTF-8
Not found any posts VIEW ALL Read more Reply Cancel reply Delete By Home PAGES POSTS View All YOU WILL ALSO LIKE: LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago