简体   繁体   English

在移动视图主页上,无法使用jQuery进行重定向

[英]On mobile view home page not redirecting using jquery

I have a website that I have built; 我有一个自己建立的网站;

http://ryanotoolecollett.com/ http://ryanotoolecollett.com/

That when resized to less than (<768px) or loaded when the window width is <768px it should redirect to; 当大小调整为小于(<768px)或在窗口宽度小于<768px加载时,应重定向到;

http://ryanotoolecollett.com/portfolio http://ryanotoolecollett.com/portfolio

I have deployed this site on a linode server and was testing it with the ip address before hand and it was working on both mobile's and pc's. 我已经将该站点部署在linode服务器上,并且事先使用ip地址对其进行了测试,并且可以在移动设备和PC上正常工作。

However now after linking the domain, the redirect works on pc's when resizing and loading <768px however on mobile devices it renders a blank page (ie landing-page:display-none; ). 但是,现在在链接域之后,重定向会在调整大小并加载<768px时在PC上起作用,但是在移动设备上它将呈现空白页(即, landing-page:display-none; )。 But this is not meant to happen. 但这并不意味着会发生。

Here is the jquery code that I have written for this (it is in my document ready function) 这是我为此编写的jQuery代码(在我的文档就绪函数中)

 $(window).on('load resize',function(event){
  event.preventDefault();

  if($(window).width() < 768 && window.location.href == 'http://ryanotoolecollett.com'){

   window.location.href = "http://ryanotoolecollett.com/portfolio";

 }

}); });

In your if statement you're doing : 在您的if语句中,您正在执行以下操作:

$(window).width() < 768 && window.location.href == 'http://ryanotoolecollett.com'

Which is fine, but window.location.href actually, equals http://ryanotoolecollett.com/ notice the forward slash at the end. 没关系,但是window.location.href实际上等于http://ryanotoolecollett.com/注意最后的斜杠。

You can do this way : 您可以这样做:

if(window.innerWidth<786)
 window.location.href = "http://ryanotoolecollett.com/portfolio";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM