0

I have a script that for some reason isn't working on mobile devices. Because of this I want to show a message to mobile devices only.

I found a PHP script that will do this but I am new to PHP and do not know how to implement this correctly.

In a PHP Page I have the following code:

<?php
  require_once 'Mobile_Detect.php';
  $detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
 <head>
   <script type="text/javascript">
     $(function() {
       if ( $detect->isMobile() ) {
         $( "body" ).prepend( "<p>You are on a mobile device.</p>" );
       }    
      });
   </script>
 </head>
 <body>
  <!-- Page Content -->
 </body>
</html>

When I run the above code it throws a syntax error.

How do I get the jQuery code to run if the device is a mobile device?

1
  • You can't just combine JavaScript & PHP, one is server side and one is client side. Currently your JavaScript is trying to parse $detect->isMobile() which fails for obvious reasons. Commented Dec 7, 2013 at 21:18

1 Answer 1

1

You are trying to use a php value in javascript without ever printing it to the page:

TRY

<?php if( $detect->isMobile() ) : ?>
<!-- only place script in page if it is mobile -->
<script type="text/javascript">
 $(function() {
     $( "body" ).prepend( "<p>You are on a mobile device.</p>" );
  });
</script>
<?php endif ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.