简体   繁体   English

使用Javascript的移动重定向网址

[英]Mobile Redirect URL with Javascript

I'm basically trying to make one link on my page and when the user clicks the link it will direct to one of these links depending on what device they are coming from. 我基本上试图在我的页面上创建一个链接,当用户点击链接时,它将指向其中一个链接,具体取决于它们来自哪个设备。

So the link says "Download Now" and I need it to launch these URL's depending on the user agent device 所以该链接显示“立即下载”,我需要它来启动这些URL,具体取决于用户代理设备

If Android then android.com
If Windows then windows.microsoft.com
If Apple then  apple.com

I am trying to avoid using PHP to accomplish this. 我试图避免使用PHP来实现这一目标。

Any suggestions? 有什么建议么?

You can use the navigator.userAgent and created a method to search for android/iPhone/WindowsPhone etc string within the userAgent string. 您可以使用navigator.userAgent并创建一个方法来搜索userAgent字符串中的android / iPhone / WindowsPhone等字符串。 You'll need to research these online and get a list of all mobile devices and redirect based on that. 您需要在线研究这些并获取所有移动设备的列表并根据该列表进行重定向。

You can use the User Agent from JavaScript and intercept clicks on links, redirecting to a page depending on what User Agent is detected. 您可以使用JavaScript中的用户代理并拦截链接上的点击,根据检测到的用户代理重定向到页面。 Here's some code to give you an idea: Change link destination based on whether user has mac or PC . 这里有一些代码可以给你一个想法: 根据用户是否有mac或PC来更改链接目标 Here's a link to various User Agents which may be useful: List of User Agents 以下是可能有用的各种用户代理的链接: 用户代理列表

Make sure that the link still has a valid href attribute though in case the user disables JavaScript. 确保链接仍具有有效的href属性,以防用户禁用JavaScript。 Another possible approach is modifying all the links: changing the href attribute on page load to fit the User Agent. 另一种可能的方法是修改所有链接:更改页面加载时的href属性以适合用户代理。

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

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