代码之家  ›  专栏  ›  技术社区  ›  Cody Raspien

正在检测移动VS桌面,未捕获URL

  •  0
  • Cody Raspien  · 技术社区  · 10 年前

    我似乎无法检测用户代理。我的URL没有正确加载iframe。

    <iframe id="link" width="100%" height="300">
      <p>Your browser does not support iframes.</p>
    </iframe>
    

     function convert() {   
    
         if (navigator.userAgent.match(/Android/i) ||
             navigator.userAgent.match(/webOS/i) ||
             navigator.userAgent.match(/iPhone/i) ||
             navigator.userAgent.match(/iPad/i) ||
             navigator.userAgent.match(/iPod/i) ||
             navigator.userAgent.match(/BlackBerry/) || 
             navigator.userAgent.match(/Windows Phone/i) || 
             navigator.userAgent.match(/ZuneWP7/i)
             ) {
    
                var url4 = "http://news.ycombinator.com";
               }
    
    
     else {
        var url4 = "lol.png";
     }  
    
    
       document.getElementById("link").src=url4;
    
    
     convert(); 
    
     }
    

    小提琴手- http://jsfiddle.net/DnRH3/

    帮助

    2 回复  |  直到 10 年前
        1
  •  0
  •   Dustin Kingen    10 年前

    你没有调用脚本。尝试: http://jsfiddle.net/DnRH3/2/

    <!DOCTYPE html>
    <html>
        <head></head>
        <body>
            <iframe id="link" width="100%" height="300">
                <p>Your browser does not support iframes.</p>
            </iframe>
            <script>
                function convert() {
                    var url = "lol.png";
    
                    if (navigator.userAgent.match(/Android/i) ||
                        navigator.userAgent.match(/webOS/i) ||
                        navigator.userAgent.match(/iPhone/i) ||
                        navigator.userAgent.match(/iPad/i) ||
                        navigator.userAgent.match(/iPod/i) ||
                        navigator.userAgent.match(/BlackBerry/) || 
                        navigator.userAgent.match(/Windows Phone/i) || 
                        navigator.userAgent.match(/ZuneWP7/i)
                    ) {
                        url = "http://news.ycombinator.com";
                    }
    
                    document.getElementById("link").src = url;
                }
    
                window.onload = convert;
            </script>
        </body>
    </html>
    
        2
  •  0
  •   Nicolae Olariu    10 年前

    当页面成功加载时,您需要这样做。试试这个 fiddle 相反

    <script>
         window.onload = convert;
    
         function convert() {   
             if (navigator.userAgent.match(/Android/i) ||
                     navigator.userAgent.match(/webOS/i) ||
                     navigator.userAgent.match(/iPhone/i) ||
                     navigator.userAgent.match(/iPad/i) ||
                     navigator.userAgent.match(/iPod/i) ||
                     navigator.userAgent.match(/BlackBerry/) || 
                     navigator.userAgent.match(/Windows Phone/i) || 
                     navigator.userAgent.match(/ZuneWP7/i))
                 {
                     var url4 = "http://news.ycombinator.com";
                 } else {
                     var url4 = "lol.png";
                 }
    
          document.getElementById("link").src=url4;
    
          //convert();
    }
    </script>