mobNav.js 818 B

123456789101112131415161718192021
  1. // 检测是否为移动端
  2. function isMobile() {
  3. return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  4. }
  5. // 如果是移动端且当前不是 mobile 页面,则重定向到移动端页面
  6. if (isMobile()) {
  7. // 如果是移动端,将 xxx.html 替换为 xxx-mob.html
  8. var currentPath = window.location.pathname;
  9. if (currentPath.endsWith('.html') && !currentPath.includes('-mob.html')) {
  10. var newPath = currentPath.replace(/\.html$/, '-mob.html');
  11. window.location.href = newPath;
  12. }
  13. } else {
  14. // 如果是桌面端,将 xxx-mob.html 替换为 xxx.html
  15. var currentPath = window.location.pathname;
  16. if (currentPath.endsWith('-mob.html')) {
  17. var newPath = currentPath.replace(/-mob\.html$/, '.html');
  18. window.location.href = newPath;
  19. }
  20. }