如果排除包含 www 的 url
我有下面的 JavaScript,当 url (window.location) 不 包含 www. javascript 已执行
var windowloc = window.location; // http://mywebsite.com/
var homeurl = "http://mywebsite.com/";
if(windowloc==homeurl){
//JavaScript IS EXECUTED
}
,如果执行,则 javascript 不执行。
var windowloc = window.location; // http://www.mywebsite.com/
var homeurl = "http://mywebsite.com/";
if(windowloc==homeurl){
//JavaScript is NOT executed.
}
如何通过允许 JavaScript 接受带或不带 www 的 url (window.location) 来克服这个问题。
I have the below JavaScript, and when the url (window.location) does not contain www. the javascript IS executed
var windowloc = window.location; // http://mywebsite.com/
var homeurl = "http://mywebsite.com/";
if(windowloc==homeurl){
//JavaScript IS EXECUTED
}
and if it does the javascript is not executed.
var windowloc = window.location; // http://www.mywebsite.com/
var homeurl = "http://mywebsite.com/";
if(windowloc==homeurl){
//JavaScript is NOT executed.
}
How can I overcome this by allowing the JavaScript to accept urls (window.location) with and without www.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用这样的代码查看域中是否有 www.mywebsite.com:
或者,您可以仅使用 window.location 的主机名部分来检查“www.”:
或者如果您想准确检查您的整个域,您可以这样做:
Use code like this see if the domain has www.mywebsite.com in it:
or, you could use just the hostname part of window.location like this to just check for the "www.":
or if you wanted to check for exactly your entire domain, you could do it like this:
您可以使用正则表达式来克服这个问题,因为我相信其他答案会提供。但是,搜索引擎优化 (SEO) 的最佳做法是强制您的
http://mywebsite.com/
永久重定向到http://www.mywebsite.com/< /code> 因为像 Google 这样的搜索引擎会考虑 www.和无 www 版本是两个独立的网站。
那么您将不需要两个单独的条件,因为您的网址将始终是 www.版本。
You can overcome that using regex, as I am sure other answers will provide. However, it's best practice for search engine optimization (SEO) to force your
http://mywebsite.com/
to do a perminant redirect tohttp://www.mywebsite.com/
because search engines like Google consider the www. and www-less versions two separate websites.Then you will not need two separate conditions because your url will always be the www. version.
编辑代码示例使其更加具体
edited the code sample to be more specific
使用
location
对象的hostname
属性来确定为您提供服务的地址:location
以及链接等其他拥有地址的对象hostname
、pathname
、search
和hash
等属性为您提供已解析的 URL 片段,因此您不必尝试自己挑选 URL 字符串。不要只是在位置字符串中查找www.
的存在,因为它可能位于字符串中不是主机名的其他位置。但是 +1 Justin 的回答:如果您尝试将替代地址(例如非 www 地址)重定向到规范地址,正确的方法是使用 HTTP 301 重定向,而不是与 JavaScript 相关。这通常在服务器级别配置,例如对于 Apache,您可以在
.htaccess
中使用Redirect
。Use the
hostname
property of thelocation
object to determine what address you're being served under:location
and other address-owning objects like links have properties likehostname
,pathname
,search
andhash
to give you the already-parsed pieces of the URL, so you don't have to try to pick apart URL strings yourself. Don't just look for the presence ofwww.
in the location string as it might be somewhere else in the string that isn't the hostname.But +1 Justin's answer: if you are trying to redirect alternative addresses such as a non-www address to a canonical address, the right way to do that is with an HTTP 301 redirect and not anything to do with JavaScript. This would normally be configured at the server level, eg for Apache you might use a
Redirect
in your.htaccess
.