Javascript 如何基于window.location显示文本?

Javascript 如何基于window.location显示文本?,javascript,html,Javascript,Html,我正在设计一个网页,如果在没有www的情况下浏览网站,我想显示一个代码片段,比如http://example.com但使用http://www.example.com <script type="text/javascript"> if (window.location.href== "http://www.example.com") { window.location.href = 'http://example.com/'; } </script> 如果(

我正在设计一个网页,如果在没有www的情况下浏览网站,我想显示一个代码片段,比如
http://example.com
但使用
http://www.example.com

<script type="text/javascript">
if (window.location.href== "http://www.example.com") {
   window.location.href = 'http://example.com/'; 
}
</script>

如果(window.location.href==”http://www.example.com") {
window.location.href=http://example.com/'; 
}
使用它,我可以将用户从www版本重定向到非www版本。 但如果有一种方法,我想添加的代码只在地址栏中的地址为
http://example.com
并在url为
http://www.example.com
。 我认为这可以使用类似的javascript代码来完成。 但是使用document.write.

您可以:

<script type="text/javascript">
window.onload = function() {
    if (window.location.href === "http://www.example.com") {
        document.getElementById("myId").style.display = "none";
        // Or, to prevent any further async loading...
        var toRemove = document.getElementById9"myId");
        toRemove.parentNode.removeChild(toRemove);
        // To populate dynamically, change below to style="display:none;" and...
        document.getElementById("myId").style.display = "block";
        document.getElementById("myId").innerHTML = "\
        .... content goes here .... \
        ";
    }
}
</script>

window.onload=函数(){
如果(window.location.href==”http://www.example.com") {
document.getElementById(“myId”).style.display=“无”;
//或者,为了防止任何进一步的异步加载。。。
var toRemove=document.getElementById9“myId”);
toRemove.parentNode.removeChild(toRemove);
//要动态填充,请在下面更改为style=“display:none;”,然后。。。
document.getElementById(“myId”).style.display=“block”;
document.getElementById(“myId”).innerHTML=”\
……内容在这里\
";
}
}
。。。后来:

<div id=myId>
... Content I want to hide ...
</div>

... 我想隐藏的内容。。。
(如果使用上述第三个选项,请改为使用空div):


这听起来像是您应该在服务器上处理的问题。 重定向到“如果用户调用”


如果您想在不同的子域上使用不同的内容,您可能需要在域提供程序中对不同的文件或目录设置路由选项。

< P>我不会认真考虑那样做。

这将不会被归类为301重定向,这将对你的搜索引擎优化不利。
如果您使用apache,请尝试使用.htaccess文件。

无论如何,您要查找的是
window.location.host

如果你想重定向(是的,但很愚蠢)


如果(window.location.host==“www.example.com”){
window.location.href=window.location.protocol+'//example.com'+window.location.pahtname+window.location.hash;
}
好的方面是,不管用户是否在线,新地址都是相同的。但糟糕的是,这将在历史记录中创建额外的步骤(后退按钮将再次重定向=无限循环),用户将加载页面两次,若js关闭,那个么它就是不工作

这是为了隐藏元素

<script type="text/javascript">
    if (window.location.host == "www.example.com") {
        document.querySelector("anything").style.display = "none";
    }
</script>

如果(window.location.host==“www.example.com”){
document.querySelector(“任何”).style.display=“无”;
}
编辑:防止www用户加载:

当没有www的用户到来时,调用同步请求。(没有js的用户看不到)


if(window.location.host==“example.com”){
var xhr=new XMLHttpRequest();
open(“GET”,“example.com/specialData”,false);
xhr.responseType=“文本”;
xhr.send();
window.onload=函数(){
document.querySelector(“div#wwwdoesntsee”).innerHTML=xhr.responseText;
};
}

谢谢你们大家。但现在我已经根据我提出的问题找到了确切的解决方案。
其特点是:
这不会让访问者感到困惑,因为我知道我希望用户看到什么,这也不会要求用户从非www重定向到www

<script>switch (window.location.host) 
{
   case "example.com":
      document.write("You are a good visitor<BR>\n")
      break 
   case "www.example.com":
      document.write("You are a bad visitor<BR>\n")
      break
   default:
      document.write("You are ALIEN<BR>\n")
      break
}</script>
开关(window.location.host)
{
案例“example.com”:
document.write(“您是一位优秀的访问者
\n”) 打破 案例“www.example.com”: document.write(“您是一个糟糕的访问者
\n”) 打破 违约: 文档。写下(“你是外星人”
\n) 打破 }
是否改为在服务器上执行此操作?:)(.htaccess也许)你的访客不在乎那种事。为他们做决定:@Windkiller No mate,我真的很想用JS来做这件事。@Avadhesh18老实说:出于几个原因,这似乎是个糟糕的主意。1) 加载页面后,它会导致不必要的重定向。2) 如果内容因您使用的子域不同而不同,则肯定会让您的用户感到困惑。3) 这可能是搜索引擎优化的噩梦。4) 这是个坏习惯。5) 使用.htaccess更简单,不必担心这个愚蠢的JavaScript。这只会隐藏内容,但不会阻止它加载。@Avadhesh18如果不想加载,请还原方法。如果用户在没有www的情况下访问example.com,则使用ajax加载其他内容。@Avadhesh18一旦窗口打开,您可以通过删除节点来防止任何异步加载(请参见上面的编辑)。如果你想阻止它加载,你要么像Windkiller建议的那样动态填充它,要么在服务器端代码中这样做@Windkiller会很好,但在编辑的代码中,我不能将switch(window.location.protocol)更改为switch(window.location.href)这是对我的问题的一个很好的回答,但它也会首先加载内容并用javascript隐藏它。我想要一些像我在编辑的问题中发布的一样的东西。顺便说一句,竖起大拇指好吧,我做了类似的事情。Ofc而不是xhr您可以使用简单的document.write或innerHTML。由于您没有指定要隐藏的部分中的确切内容,所以我制作了全局解决方案。。。
<script type="text/javascript">
    if (window.location.host == "www.example.com") {
        document.querySelector("anything").style.display = "none";
    }
</script>
<script type="text/javascript">
    if (window.location.host == "example.com") {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "example.com/specialData", false);
        xhr.responseType = "text";
        xhr.send();
        window.onload = function () {
            document.querySelector("div#wwwdoesntsee").innerHTML = xhr.responseText;
        };
    }
</script>
<script>switch (window.location.host) 
{
   case "example.com":
      document.write("You are a good visitor<BR>\n")
      break 
   case "www.example.com":
      document.write("You are a bad visitor<BR>\n")
      break
   default:
      document.write("You are ALIEN<BR>\n")
      break
}</script>