[英]Redirect domain to www Javascript
How i can redirect a domain to www in javascript 我如何在JavaScript中将域名重定向到www
http://example.com to http://www.example.com http://example.com到http://www.example.com
and this is my code 这是我的代码
<script>
if(/http:\/\/?example\.com/.test(document.referrer)) {
window.location = "http://www.example.com/";
}
</script>
thank you 谢谢
<script>
if(window.location.hostname === "example.com") {
window.location.hostname = "www.example.com";
}
</script>
You should use window.location.hostname
because document.referrer
doesn't always work. 您应该使用
window.location.hostname
因为document.referrer
并不总是有效。 If you don't believe me, try it in the inspector of this page. 如果您不相信我,请在此页面的检查器中尝试。 So as you will have something like the one of @Chris :
因此,您将拥有类似于@Chris的东西:
if(window.location.hostname === "example.com") {
window.location.hostname = "www.example.com";
}
And avoid regex when it's possible 并尽可能避免使用正则表达式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.