我需要帮助吗?我想让我的网站有一个桌面和移动版本。将样式设置为元素的正确宽度的CSS代码很容易,但URL更难。我想知道的是,如何制作一个 URL ,它会导致桌面上的主页,但是在移动平台上,这个URL是不同的,但是它与桌面版本的主页相同,但在移动版本中。您是否知道是否有任何代码可以做到这一点,而不会在预览时出现任何错误。我不介意你的答案是用PHP还是JavaScript等等。
这是我试图使用的代码。我不确定它是否成功。不能说有可能。
$(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);
}
});
});你能帮帮我吗?记住,我想要一些帮助,在制作移动重定向URL和桌面URL,但这两者将导致相同的页面,但在不同的版本请。
发布于 2017-10-07 10:55:44
您可以使用浏览器发送的用户代理头来检查用户是移动用户还是桌面用户,并根据返回的值进行重定向。您可以使用-
var usrAgent = navigator.userAgent;现在您知道了用户代理,检查返回的用户代理是否可移动,并相应地重定向-
function detectMobile()
{
if(usrAgent.match(/Mobile/i))
{
return "Mobile";
}
}
var isMobile = detectMobile();
if(isMobile)
{
window.location = "MOBILE_URL";
}
else
{
window.location = "DESKTOP_URL";
}或者,
您可以使用html 链接标记根据用户设备的宽度重定向到任何网页。
<link rel="alternate" media="only screen and (max-width: 640px*)" href="MOBILE_URL_TO_REDIRECT">*根据您的要求更改此宽度。640 max是移动设备的理想最大宽度。
我希望这能解决你的问题。
https://stackoverflow.com/questions/46618417
复制相似问题