如果排除包含 www 的 url

发布于 2024-12-22 17:25:12 字数 599 浏览 3 评论 0原文

我有下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夕嗳→ 2024-12-29 17:25:12

使用这样的代码查看域中是否有 www.mywebsite.com:

if (window.location.href.indexOf("//www.mywebsite.com/") != -1) {
     // code to execute if it is www.mywebsite.com
} else {
     // code to execute if it is not www.mywebsite.com
}

或者,您可以仅使用 window.location 的主机名部分来检查“www.”:

if (window.location.hostname.indexOf("www.") != -1) {
     // code to execute if it is www. something
} else {
     // code to execute if it is not www. something
}

或者如果您想准确检查您的整个域,您可以这样做:

if (window.location.hostname === "www.mywebsite.com" {
     // code to execute if it is www.mywebsite.com
} else {
     // code to execute if it is not www.mywebsite.com
}

Use code like this see if the domain has www.mywebsite.com in it:

if (window.location.href.indexOf("//www.mywebsite.com/") != -1) {
     // code to execute if it is www.mywebsite.com
} else {
     // code to execute if it is not www.mywebsite.com
}

or, you could use just the hostname part of window.location like this to just check for the "www.":

if (window.location.hostname.indexOf("www.") != -1) {
     // code to execute if it is www. something
} else {
     // code to execute if it is not www. something
}

or if you wanted to check for exactly your entire domain, you could do it like this:

if (window.location.hostname === "www.mywebsite.com" {
     // code to execute if it is www.mywebsite.com
} else {
     // code to execute if it is not www.mywebsite.com
}
寻找我们的幸福 2024-12-29 17:25:12

您可以使用正则表达式来克服这个问题,因为我相信其他答案会提供。但是,搜索引擎优化 (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 to http://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.

if (window.location.href.indexOf("://www") === -1) {
    // "www" excluded
} else {
    // other stuff
}

编辑代码示例使其更加具体

if (window.location.href.indexOf("://www") === -1) {
    // "www" excluded
} else {
    // other stuff
}

edited the code sample to be more specific

雨轻弹 2024-12-29 17:25:12
if(window.location.href.indexOf('mywebsite.com')!= -1){
   //do stuff
}
if(window.location.href.indexOf('mywebsite.com')!= -1){
   //do stuff
}
爱冒险 2024-12-29 17:25:12

使用 location 对象的 hostname 属性来确定为您提供服务的地址:

if (location.hostname==='mywebsite.com')
    // do something

location 以及链接等其他拥有地址的对象hostnamepathnamesearchhash 等属性为您提供已解析的 URL 片段,因此您不必尝试自己挑选 URL 字符串。不要只是在位置字符串中查找 www. 的存在,因为它可能位于字符串中不是主机名的其他位置。

但是 +1 Justin 的回答:如果您尝试将替代地址(例如非 www 地址)重定向到规范地址,正确的方法是使用 HTTP 301 重定向,而不是与 JavaScript 相关。这通常在服务器级别配置,例如对于 Apache,您可以在 .htaccess 中使用 Redirect

Use the hostname property of the location object to determine what address you're being served under:

if (location.hostname==='mywebsite.com')
    // do something

location and other address-owning objects like links have properties like hostname, pathname, search and hash 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 of www. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文