jQuery add class based on page URL

You can use window.location to get the current URL, and then switch based on that: $(function() { var loc = window.location.href; // returns the full URL if(/technology/.test(loc)) { $(‘#main’).addClass(‘tech’); } }); This uses a regular expression to see if the URL contains a particular phrase (notably: technology), and if so, adds a class to the … Read more

Encode URL query parameters

URLEncoder has a very misleading name. It is according to the Javadocs used encode form parameters using MIME type application/x-www-form-urlencoded. With this said it can be used to encode e.g., query parameters. For instance if a parameter looks like &/?# its encoded equivalent can be used as: String url = “http://host.com/?key=” + URLEncoder.encode(“&/?#”); Unless you … Read more

Get specific subdomain from URL in foo.bar.car.com

Given your requirement (you want the 1st two levels, not including ‘www.’) I’d approach it something like this: private static string GetSubDomain(Uri url) { if (url.HostNameType == UriHostNameType.Dns) { string host = url.Host; var nodes = host.Split(‘.’); int startNode = 0; if(nodes[0] == “www”) startNode = 1; return string.Format(“{0}.{1}”, nodes[startNode], nodes[startNode + 1]); } return … Read more

Extract domain name from URL in Python

Use tldextract which is more efficient version of urlparse, tldextract accurately separates the gTLD or ccTLD (generic or country code top-level domain) from the registered domain and subdomains of a URL. >>> import tldextract >>> ext = tldextract.extract(‘http://forums.news.cnn.com/’) ExtractResult(subdomain=’forums.news’, domain=’cnn’, suffix=’com’) >>> ext.domain ‘cnn’