如何使用Javascript从移动网站链接到完整网站,而无需重定向回移动设备

时间:2013-02-19 20:19:01

标签: javascript redirect mobile web

我从我的完整网站的所有网页调用此脚本,将移动用户重定向到移动网站网址。 当有人点击链接(从移动网站)查看完整网站时,问题就出现了......重定向会将用户权限移回移动网站。

<script type="text/javascript">
<!--
if (screen.width <= 700) {
window.location = "http://domain.com";
}
//-->
</script>

如果用户来自移动网站网址,我是否可以添加到此JS代码中以禁止重定向? 我只想在我的情况下使用javascript,所以如果有人有JS解决方案;那就是我要找的东西。感谢。

2 个答案:

答案 0 :(得分:1)

使用来自MDN的Cookie代码:https://developer.mozilla.org/en-US/docs/DOM/document.cookie

点击链接。设置一个cookie

docCookies.setItem("isMobile","true");

当您检查重定向时,请阅读cookie

if (!docCookies.getItem("isMobile"))

答案 1 :(得分:1)

这在某种程度上,它将记住用户选择100天。 http://www.w3schools.com/js/js_cookies.asp

使用我提供的链接中的功能:然后更改此项;

<script type="text/javascript">
<!--
if (screen.width <= 700 && getCookie("mobileRedirected")!="true"){
window.location = "http://domain.com";
}
//-->
</script>

在返回该页面的链接上执行:

<a href="#" onclick="setCookie('mobileRedirected','true',100);document.location='index.html';">LINK</a>

下次用户进行索引时,会知道它已经点击了他离开mobile.html的方式

如果您希望用户能够在网站之间进行选择,如果没有(仅显示移动设备一次),您可以这样做:

   <script type="text/javascript">
    <!--
    if (screen.width <= 700 && getCookie("mobileRedirected")!="true"){
setCookie('mobileRedirected','true',100);
    window.location = "http://domain.com";
    }
    //-->
    </script>

并一起跳过链接代码

上面代码的完整工作示例

   <script type="text/javascript">
   function setCookie(c_name,value,exdays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name){
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++){
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)    {
    return unescape(y);
    }
  }
}
    if (screen.width <= 700 && getCookie("mobileRedirected")!="true"){
        setCookie('mobileRedirected','true',100);
        window.location = "http://google.com";
    }
    </script>

如果您希望重定向小型浏览器窗口,也可以使用window.innerWidth。 工作示例链接:http://allanthya.net/cookietest.php