IT Certificaions

Google Cloud Platform

Thursday, March 15, 2012

Get device type on accessing your website

Find which device has accessed your website: See the javascript code below


function GetDeviceType(){
           if( navigator.userAgent.match(/Android/i) ||
              navigator.userAgent.match(/webOS/i) ||
              navigator.userAgent.match(/iPhone/i) ||
              navigator.userAgent.match(/iPod/i) ||
              navigator.userAgent.match(/BlackBerry/)
            ){
               alert('Device type is ::'+navigator.userAgent);
            }else if(navigator.userAgent.match(/Windows/)){
   alert(' Device type is :: Windows:::'+navigator.userAgent);
}else if(navigator.userAgent.match(/Linux/)){
   alert(' Device type is : Linux:::'+navigator.userAgent);
}else{
              alert(' Other Device:::'+navigator.userAgent);
}


 }

If somebody knows Jquery code for this, update this post or comment here.

Wednesday, March 14, 2012

Choosing random image using jquery

You must include jquery js in your code. Now define a header css class in <head>


<style type="text/css">
          #header {height: 250px};
 </style>

Now define a div inside your <body> tag where jquery append randomly images..
<div id="banner"> </div>

Now put this code at the bottom of your code before </html> tag...

<script type="text/javascript">
    var images = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg', 'img5.jpg'];

    $(document).ready(function() {
          
            $('#header').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});
        
          $('<img src="images/' + images[Math.floor(Math.random() * images.length)] + '">').appendTo('#banner');
 
  });

</script>

Here we have placed these images(img1,img2,img3,img4 and img5.jpg)  inside images directory.

Choose random image on page load



Let's suppose you are using following image tag...

<img src="images/home_1.jpg" id="imageSelecter" alt="Pick random image" />


Now add the following javascript code at the bottom of your page before closing of  HTML tag.

<script type="text/javascript">
       var images =new Array('images/home_1.jpg', 'images/home_2.jpg', 'images/home_3.jpg', 'images/home_4.jpg', 'images/home_5.jpg');
          
      var len = images.length;
     var random_no = Math.floor(len*Math.random());
     document.getElementById("imageSelecter").src = images[random_no];

</script>