Home >Web Front-end >JS Tutorial >Two JS scripts determine the mobile browser type and jump to the WAP mobile website_javascript skills
With the popularity of mobile devices, corporate online promotions can no longer be limited to the PC side, but need to make achievements on the mobile side at the same time. For company websites, they used to be made on PC. Of course, mobile terminals such as mobile phones can also be accessed, but the user experience is definitely not as convenient as a completely suitable mobile terminal. After we build a WAP mobile website for our website, if a user accesses our corporate top-level domain name website through a mobile phone, we will jump to the specially designed WAP website.
Here, Lao Zuo has sorted out two JS scripts that he is currently using. Because friends have been asking me for them before, I share them here.
The first type: direct JS script
<script type="text/javascript"> try { var urlhash = window.location.hash; if (!urlhash.match("fromapp")) { if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) { window.location="http://www.laozuo.org(更换成自己的WAP网站)"; } } } catch(err) { }</script>
The second, slightly longer, quote
function uaredirect(murl){ try { if(document.getElementById("bdmark") != null){ return; } var urlhash = window.location.hash; if (!urlhash.match("fromapp")){ if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) { location.replace(murl); } } } catch(err){} }
Save the above script to the mobile.js file, and then reference the following 2 JS call files on the page that needs to be called.
<script src="mobile.js" type="text/javascript"></script> <script type="text/javascript">uaredirect("http://www.laozuo.org(更换成自己的WAP网站)");</script>
After testing by myself, both js scripts can determine the mobile browser type and jump to the WAP mobile website. Friends who need it can use it directly.