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 :slight_smile:

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?