O'Reilly logo

Yii Rapid Application Development Hotshot by James R. Hamilton III, Lauren J. O'Meara

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Detecting Mobile Browser

When a browser connects, we should determine if it is a mobile browser. We will need to write some code to do this.

Engage Thrusters

  1. Open ch2 | SourceFiles | protected | views | layouts | main.php and add these lines in the head section after the css includes and before the title tag:
    <?php 
        Yii::app()->clientScript->registerCoreScript('jquery'); 
        Yii::app()->clientScript->registerScriptFile(
          Yii::app()->request->baseUrl . '/js/detectmobilebrowser.js'
        ); 
      ?>

    This will load jQuery (it comes with Yii) and our custom browser detection script.

  2. Now, to test our handiwork, temporarily add the following lines at the beginning of the mainmenu div, after the opening php tag:
     Yii::app()->clientScript->registerScript('detectmobilebrowser'," ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required