简体   繁体   English

移动网址重定向和桌面网址重定向

[英]Mobile URL Redirect and Desktop URL Redirect

I need help please? 我需要帮助吗? I want to make my website that has a desktop and a mobile version. 我要使我的网站具有台式机和移动版。 The CSS coding of styling to the correct widths of HTML elements are easy but a URL is harder. 样式化为HTML元素的正确宽度的CSS编码很容易,但是URL很难。 I want to know is that how to make a URL that leads to a home page on desktop but on mobile the URL is different but it goes to the same home page as the desktop version but in the mobile version. 我想知道的是,如何制作一个指向台式机主页的URL ,但在移动设备上该URL是不同的,但与台式机版本的URL相同,但在移动版本中的URL却指向同一主页。 Do you know if there is any code that can do that with out getting any errors on previewing it privately. 您知道是否有任何代码可以做到这一点,而在私下预览时不会出现任何错误。 I don't mind if your answers are using PHP or JavaScript etc. 我不介意您的答案是使用PHP还是JavaScript等。

This is a code that i tried to use. 这是我尝试使用的代码。 I wasn't sure if it worked or not. 我不确定它是否有效。 Can't say it possibly did. 不能说有可能。

$(document).ready(function() {
    $(window).on("load resize", function() {
        if($(window).width() <= 950) {
            var mobile = window.location = "/mindex.php";
            console.log("The location is " + mobile);
        } else if($(window).width() > 950) {
            var desktop = window.location = "/index.php";
            console.log("The location is " + desktop);
        } else {
            var defaultPlace = "";
            console.log("Defualt location: " + defaultPlace);
        }
    });
});

Can you please help me? 你能帮我么? Remember, I would like some help on making a mobile redirect URL and a desktop URL but that will both lead to the same page but in different versions please. 记住,我想要一些有关制作移动重定向URL和桌面URL的帮助,但这两者都将导致相同的页面,但请使用不同的版本。

You can use user-agent header sent by the browser to check whether the user is mobile user or desktop user and redirect according to the returned value. 您可以使用浏览器发送的用户代理标头来检查用户是移动用户还是台式机用户,并根据返回的值进行重定向。 You can check user-agent header using this - 您可以使用以下方法检查用户代理标头-

var usrAgent = navigator.userAgent;

Now you know the user agent, check the returned user-agent is mobile or not and redirect accordingly - 现在您知道了用户代理,请检查返回的用户代理是否可移动,并进行相应的重定向-

function detectMobile() 
{
    if(usrAgent.match(/Mobile/i))
    {
        return "Mobile";
    }
}

var isMobile = detectMobile();

if(isMobile)
{
    window.location = "MOBILE_URL";
}
else
{
    window.location = "DESKTOP_URL";
}

Alternatively, 或者,

You can use html link tag to redirect to any webpage according to user device width using this - 您可以使用html 链接标签根据用户设备的宽度重定向到任何网页,方法是:

<link rel="alternate" media="only screen and (max-width: 640px*)" href="MOBILE_URL_TO_REDIRECT">

*Change this width according to your requirement. *根据您的要求更改此宽度。 640px is the ideal max width for mobile devices. 640像素是移动设备的理想最大宽度。

I hope this solves your problem. 我希望这能解决您的问题。

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

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