I'm trying to trim the values of an href before and after the domain name using JavaScript. For example, http://www.google.com/about-us should be trimmed to www.google.com.
var str = "http://www.google.com/about-us";
var str_before = str.replace("http://","");
document.write(str_before); // Returns ("www.google.com/about-us")
// Trim everything after the domain name
var link = str_before.substring(0, str_before.indexOf('/'));
document.write(link); // Returns "www.google.com/about-uswww.google.com"
I don't know why this is happening. Any help would be greatly appreciated!
var a = document.createElement("a"); a.href = str; a.host; // "www.google.com"document.writetricking you. Useconsole.logto check the results of your code.dw(). I've left feedback for Mozilla a couple of years ago about usingdw()in examples, but not sure, if its use has been decreased.