Home  >  Q&A  >  body text

javascript - How to load different html on the JD homepage on the PC and mobile terminals?

Using a mobile browser to access JD.com’s domain name is http://m.jd.com
Using a computer to access JD.com is http://www.jd.com. Of course, the two pages are different. What’s wrong with this? Realized?
Write an example code if it is convenient, thank you!

女神的闺蜜爱上我女神的闺蜜爱上我2651 days ago927

reply all(5)I'll reply

  • 怪我咯

    怪我咯2017-06-17 09:18:32

    After entering www.jd.com, if the code determines that it is a mobile phone, it will jump to m.jd.com

    let ua = window.navigator.userAgent.toLocaleLowerCase()
    let murl ="//m.jd.com",
    let reg =/iphone|android|symbianos|windows\sphone/g
    
    if (reg .test(ua )) {
        window.location.href = murl 
    }

    JD source code:

    !function(n){function o(n){for(var o=n+"=",t=document.cookie.split(";"),e=0;e<t.length;e++){for(var i=t[e];" "==i.charAt(0);)i=i.substring(1,i.length);if(0==i.indexOf(o))return i.substring(o.length,i.length)}return null}var t=o("pcm"),e=n.navigator.userAgent.toLocaleLowerCase(),i="//m.jd.com",r=/iphone|android|symbianos|windows\sphone/g,c=/micromessenger|qq\/[\d.]+/i;return c.test(e)?(n.location.href="//wqs.jd.com/?from=jdindex",!1):r.test(e)&&"1"!=t?(n.location.href=i,!1):void 0}(window);

    reply
    0
  • 阿神

    阿神2017-06-17 09:18:32

    Just judgeUA

    reply
    0
  • ringa_lee

    ringa_lee2017-06-17 09:18:32

    request.getHeader("user-agent")

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-17 09:18:32

    userAgent

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-17 09:18:32

    window.navigator.userAgent

    reply
    0
  • Cancelreply