Need Help With Iphone/ipad Website Script

Discussion in 'JavaScript' started by larssonk22, Jan 12, 2012.

  1. #1
    A member here has given me a code to sniff out ipad and iphone users on my site.

    in_array("ipad",$_SERVER[http_user_agent]); 
    Code (markup):
    But I don't know how to use this to tell iphones and ipads to run my script.

    <script type="text/javascript" src="script/iscroll.js"></script>
    <script type="text/javascript">
    var myScroll;
    function loaded() {
    setTimeout(function () {
        myScroll = new iScroll('scroll-pane', { zoom: true, snap:true, hScrollbar: false, vScrollbar: false, zoomMax: 2, momentum: true });
         }, 100);  
    }
    window.addEventListener('load', loaded, false);
    </script>
    PHP:
    Alternatively, if I could tell IE9 not to run this script that would be good too
     
    Solved! View solution.
    larssonk22, Jan 12, 2012 IP
  2. #2
    that's php..

    here's a javascript version..
    
    
    var is_iPad = /iPad/i.test(navigator.userAgent);
    var is_iPhone = /iPhone/i.test(navigator.userAgent);
    var is_iPod = /iPod/i.test(navigator.userAgent);
    
    
    Code (markup):
    here's how to integrate with your code..
    
    <script type="text/javascript" src="script/iscroll.js"></script>
    <script type="text/javascript">
    
    var is_iPad = /iPad/i.test(navigator.userAgent);
    var is_iPhone = /iPhone/i.test(navigator.userAgent);
    var is_iPod = /iPod/i.test(navigator.userAgent);
    
    var myScroll;
    function loaded() {
      if(is_iPad || is_iPhone || is_iPod ){
        setTimeout(function () {
            myScroll = new iScroll('scroll-pane', { zoom: true, snap:true, hScrollbar: false, vScrollbar: false, zoomMax: 2, momentum: true });
             }, 100);  
      }
    }
    window.addEventListener('load', loaded, false);
    </script>
    
    HTML:
     
    JohnnySchultz, Jan 12, 2012 IP
  3. larssonk22

    larssonk22 Well-Known Member

    Messages:
    236
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Thank you so much JohnnySchultz! Worked perfectly :cool:
     
    larssonk22, Jan 13, 2012 IP
  4. AntC

    AntC Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How can I do the opposite and NOT run a particular script if the user is on an iPhone/iPad?
     
    AntC, Jun 28, 2012 IP
  5. XP1

    XP1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Do you want client-side solution or server-side solution? What language are you using on the server side?
     
    XP1, Jun 28, 2012 IP
  6. AntC

    AntC Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just building the pages with HTML (no PHP or anything like that involved). Was hoping there might be an equivalent to CSS media types where when viewed on certain devices a certain bit of code isn't applied.

    It's just to disable one particular bit of javascript if not viewed on a computer.
     
    AntC, Jun 28, 2012 IP
  7. XP1

    XP1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    At the top of your function, you could put an early return statement.
    if (isIphone || isIpad)
    {
        return;
    }
    Code (markup):
     
    XP1, Jun 28, 2012 IP