admin管理员组文章数量:1384106
The code below only shows <span>
on / but wont show <span>
on .html so how can I get it to work on all pages with the specified domain? Please help.
<script type="text/javascript">
var myurl = "/";
var currenturl = window.location
if(myurl != currenturl) {
$("<span style=font-size:200px;>big</span>").replaceAll("body"); // check replaceWith() examples
}
</script>
The code below only shows <span>
on http://example./ but wont show <span>
on http://example./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./";
var currenturl = window.location
if(myurl != currenturl) {
$("<span style=font-size:200px;>big</span>").replaceAll("body"); // check replaceWith() examples
}
</script>
Share
Improve this question
edited Jan 8, 2021 at 15:32
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Oct 2, 2011 at 22:25
user975763user975763
351 gold badge3 silver badges8 bronze badges
2 Answers
Reset to default 3This should work:
<script type="text/javascript">
var myurl = "www.myurl.";
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/en/window.location
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 paring a string and an object using the equals operator, the string is pared 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./ 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.) 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 parison 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.
本文标签:
版权声明:本文标题:javascript - If domain specified not equal to then current URL apply this jQuery as well as pages with same domain - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744523658a2610610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论