10 common JavaScript codes for mobile website development

Source: Internet
Author: User

1. If the webpage is viewed on the iPhone or Android browser, add the "iPhone" or "Android" class name to the subject element.

 
 
  1. if (navigator.userAgent.match(/iPhone/i)) {  
  2.     $('body').addClass('iPhone');  
  3. } else if (navigator.userAgent.match(/Android/i)) {  
  4.         $('body').addClass('Android');  

IPhone user browsing example:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420 + (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3
Mozilla/5.0 (iPhone; U; XXXXX like Mac OS X; en) AppleWebKit/420 + (KHTML, like Gecko) Version/3.0 Mobile/1A477d Safari/419.3

Android user browsing example:

Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 1.6; en-gb; Dell Streak Build/Donut AppleWebKit/528.5 + (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1
Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17
Mozilla/5.0 (Linux; U; Android 2.2; en-us; DROID2 GLOBAL Build/S273) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; E10i Build/2.0.2.A.0.24) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

2. Remove the browser address bar

 
 
  1. window.scrollTo(0, 1); 

3. Prevent webpage touch scrolling

 
 
  1. notouchmove = function(event) {  
  2.     event.preventDefault();  
  3. }  
  4. <div data-role="page" id="home" ontouchmove="notouchmove(event);">  
  5. ...  
  6. </div> 

4. Information displayed during horizontal browsing

 
 
  1. var updateorientation = function (){  
  2.     var classname = '',  
  3.     top = 100;  
  4.     switch(window.orientation){  
  5.         case 0:  
  6.         classname += "normal";  
  7.         break;  
  8.  
  9.         case -90:  
  10.         classname += "landscape";  
  11.         break;  
  12.  
  13.         case 90:  
  14.         classname += "landscape";  
  15.         break;  
  16.  
  17.     }  
  18.  
  19.     if (classname == 'landscape') {  
  20.         if ($('#overlay').length === 0) {  
  21.             window.scrollTo(0, 1);  
  22.             $('body').append('<div id="overlay" style="width: 100%; height:' + $(document).height() + 'px"><span style="top: ' + top + 'px">Landscape view is not supported for this page.</span></div>');  
  23.         }  
  24.     } else {  
  25.         $('#overlay').remove();  
  26.     }  
  27. };  
  28. Usage:  
  29.  
  30. var supportsOrientationChange = "onorientationchange" in window,  
  31. orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";  
  32.  
  33. window.addEventListener(orientationEvent, function() {  
  34.     updateorientation();  
  35. }, false); 

5. Partial description information is displayed. complete information is displayed when you click.

 
 
  1. var truncatedesc = function(trunc, len) {  
  2.     if (trunc) {  
  3.       var org = trunc;  
  4.  
  5.       if (trunc.length > len) {  
  6.         trunc = trunc.substring(0, len);  
  7.         trunc = trunc.replace(/w+$/, '');  
  8.  
  9.         trunc = '<span class="truncated">' + trunc;  
  10.         trunc += '<strong class="more-description">...</strong></span>';  
  11.         trunc += '<span class="original" style="display: none;">' + org + '</span>';  
  12.       }  
  13.  
  14.       $('.truncated').live("touchstart touchend", function() {  
  15.         $(this).closest('div').find('.original').show();  
  16.         $(this).closest('div').find('.truncated').hide();  
  17.         return false;  
  18.       });  
  19.  
  20.       return trunc;  
  21.     }  
  22. };  
  23.  
  24. Usage:  
  25.  
  26. truncatedesc(item.description, 100); 

6. When you receive a successful Ajax request, redirect to another page (jQuery mobile)

 
 
  1. var ajaxurl = ‘http://…’; // Your web service URL  
  2.  
  3. $.ajax({  
  4.     url: ajaxurl,  
  5.     type: 'GET',  
  6.     processData: false,  
  7.     contentType: "application/json",  
  8.     dataType: "jsonp",  
  9.     success: function(data) {  
  10.         $.mobile.changePage("results.html");  
  11.     },  
  12.     error: function() {  
  13.         alert('Error!');  
  14.     }  
  15. }); 

7. Delete the activity status (jQuery mobile) from the link in the List View)

 
 
  1. $('div').live('pageshow', function (event, ui) {  
  2.     $('[data-role=listview] li').removeClass("ui-btn-active");  
  3. }); 

8. Disable the default jQuery mobile style from the drop-down list)

 
 
  1. $(document).bind("mobileinit", function(){  
  2.      $.mobile.page.prototype.options.keepNative = "select";  
  3. }); 

9. Dynamic Update List View (jQuery mobile)

 
 
  1. var output  = '<li>';  
  2. output += '
  3. output += '</li>';     
  4.  
  5. $('#mylistul').append(output).listview('refresh');  

10. dynamically Add the form input and default apply style (jQuery mobile)

 
 
  1. var html = '<input type="search" name="suburb" id="suburb" placeholder="Enter suburb" />';  
  2. $('#searchform').append(html);  
  3. $('#suburb').textinput(); 

Original article: http://qing.weibo.com/1609119537/5fe93731330004ep.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.