0

The code below only shows <span> on http://example.com/ but wont show <span> on http://example.com/files/target.html so how can I get it to work on all pages with the specified domain? Please help.

<script type="text/javascript">
var myurl = "http://example.com/";
var currenturl = window.location
if(myurl != currenturl) {
$("<span style=font-size:200px;>big</span>").replaceAll("body"); // check replaceWith() examples
}
</script>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user975763
  • 35
  • 3
  • 8

2 Answers2

3

This should work:

<script type="text/javascript">
    var myurl = "www.myurl.com";
    var currenturl = window.location.hostname;
    if(myurl != currenturl) {
        $("<span style=font-size:200px;>big</span>").replaceAll("body"); // check replaceWith() examples
    }
</script>

Per MDN Docs: https://developer.mozilla.org/en/window.location

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • I suppose that's a good thing, since `window.location.hostname` should always equal `www.myurl.com`, and if not, something is wrong. What exactly are you trying to do? – AlienWebguy Oct 02 '11 at 22:52
  • Prevent someone from stealing my javascript because within it will be this an then they're blog will not function propperly. seems harsh but :/ :). – user975763 Oct 03 '11 at 17:10
  • 1
    You can't prevent someone from stealing your javascript. It's on the client. Stop trying :) – AlienWebguy Oct 03 '11 at 22:58
2

What you wrote doesn't work because window.location returns a Location object, which is a host object. The variable myurl is a string. When comparing a string and an object using the equals operator, the string is compared with the result of calling the object's toString method.

Host objects don't necessarily have a toString method, so attempting to call it could throw an error. Even if the location object of the browser has a toString method, it could return a string that is the value of any one of those properties, or something else.

As it happens, in most browsers window.location.toString() will return the current URL (which is specified in Moziall's Gecko DOM Reference). However, myurl contains the string http://myurl.com/ and the URL usually contains more information, such as the current page being displayed.

To match myurl, you need the protocol (http:) separator (//), hostname (myurl.com) and a trailing "/" character, so:

var loc = window.location;
myurl = loc.protocol + '//' + loc.hostname + '/';

Or you could format myurl to match one of the properties of the location object to make the comparison simpler.

PS. HTML5 is the first attempt at standardising the window object across browsers, so expect it to be a little different in different browsers—program defensively and test widely.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • sorry to be a pain but I cant seem too get it to work could you please show me with the url in the correct place please. – user975763 Oct 03 '11 at 17:23
  • I have it working now http://stackoverflow.com/questions/1368264/get-root-url-in-JavaScript fount the code from here very similar . thank you very much RobG I actually really appreciate it. thank you. thank you. – user975763 Oct 03 '11 at 17:31