我有下面一效果:
<script>
var thisurl=window.location.href;
if (thisurl.indexOf("www")==-1) {
   window.location.href="http://www.aaa.com/";
}
</script>上面是判断当前的URL有没有WWW,如果没有就统一跳到http://www.aaa.com/。
现在我想改为这样的效果:
判断当前的URL有没有WWW,如果没有就自动加上WWW,然后再转跳,
例如:
如果是http://abc.com/news/list.htm
然后自动加上www,再转跳到http://www.abc.com/news/list.htm请问怎么修改上面的JS代码?

解决方案 »

  1.   


    <script>
    var thisurl=window.location.href;
    if (thisurl.indexOf("www.")==-1) {
      thisurl=thisurl.replace(/(http\:\/\/)(.+)/ig,"$1www.$2")
      window.location.href=thisurl;
    }
    </script>
      

  2.   


    var host=location.host,pathname=location.pathname;if (host.split('.').length==2)//为顶级域名
      window.location.href="http://www."+host+pathname;
      

  3.   

    var thisurl=window.location.href;
    if (thisurl.indexOf("www")==-1) {
      window.location.href=thisurl.replace(/(http\:\/\/)/i,'http://www');
    }