Question

I cannot seem to be able to detect user agent. My URL isn't loading up the iframe properly.

<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(); 

 }

Fiddle - http://jsfiddle.net/DnRH3/

Help!

Was it helpful?

Solution

You're not calling the script. Try: 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>

OTHER TIPS

You need to do that when page is successfully loaded. Try this fiddle instead.

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top