More on this here[
^]
You need location.hostname to get the root url. You're using location.pagename which includes the path and page executing the JavaScript.
These properties do not append a slash so you will need to prepend that to the string.
You can debug JavaScript in chrome and in Firefox using firebug. There are other debuggers out there but I don't rate them.
Failing that, you can always add an alert(string) to your code so you can see exactly what a property returns
Hope that helps ^_^
Don't forget to rate an subscribe ;P