SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Technically, a bit dim macdan's Avatar
    Join Date
    Feb 2001
    Location
    London
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detecting an ipad/ipod/iphone

    Hi

    Wonder if anyone can help me...

    I'm trying to swap flash for an image when a user is on an ipod/ipad/iphone. This snippet works:

    <script type="text/javascript">
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
    {document.write ('you are an ipod')
    }
    else
    document.write('you are not an ipod')
    </script>

    but when I enter in the content for non ipods (a javascript call) it trips up:

    <script type="text/javascript">
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
    {document.write ('you are an ipod')
    }
    else
    document.write('<script language="javascript">RunFoo2();</script>')
    </script>

    Would be grateful for any ideas or hints :-)
    gorillaweb is a small London based digital design agency.

  2. #2
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cannot do what you want using document.write . You must use createElement to create a "script" tag, then creatTextNode that contains the JS you want to execute, then appendChild the text node to the script node you created, and finally appendChild the the script tag to the document body.

    May I ask why you don't just execute the JS, instead of appending the the JS call you want to make in the page?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •