JavaScript code used to develop a mobile web (rpm)

Reprinted: http://mobile.51cto.com/web-321960.htm

1. If the page is viewed in the iPhone or Android browser, add "iPhone" or "Android" class name in the body element

Javascript code

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

iPhone users browse examples:

  • 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 users browse examples:

  • 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

window.scrollTo(0, 1);

3. To prevent the page scroll touch

notouchmove = function(event) {   
    event.preventDefault();   
}   

<div data-role="page" id="home" ontouchmove="notouchmove(event);"> 
...   
</div>   

4. When the display information browsing lateral

var updateorientation = function (){   
    var classname = '',  
    top = 100;
    switch(window.orientation){  
        case 0: classname += "normal";break;
case -90:
classname += "landscape";break;
case 90:classname += "landscape";break; } if (classname == 'landscape') { if ($('#overlay').length === 0) { window.scrollTo(0, 1); $('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>'); } } else { $('#overlay').remove(); } }; Usage: var supportsOrientationChange = "onorientationchange" in window, orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; window.addEventListener(orientationEvent, function() { updateorientation(); }, false);

The display section describes the information, the display information is complete when clicked

var truncatedesc = function(trunc, len) { 
    if (trunc) {  
      var org = trunc;  
      if (trunc.length > len) {
        trunc = trunc.substring(0, len);  
        trunc = trunc.replace(/w+$/, ''); 
        trunc = '<span class="truncated">' + trunc;
        trunc += '<strong class="more-description">...</strong></span>';
        trunc += '<span class="original" style="display: none;">' + org + '</span>'; 
      } 
      $('.truncated').live("touchstart touchend", function() { 
        $(this).closest('div').find('.original').show(); 
        $(this).closest('div').find('.truncated').hide();
        return false;   
      });  
      return trunc; 
    }  
};   
Usage: truncatedesc(item.description, 100);   

6. Upon receipt of the successful Ajax request is redirected to another page (jQuery mobile)

var ajaxurl = ‘http://…’; // Your web service URL  
$.ajax({
    url: ajaxurl,  
    type: 'GET',  
    processData: false,  
    contentType: "application/json",  
    dataType: "jsonp",  
    success: function(data) {   
        $.mobile.changePage("results.html"); 
    },   
    error: function() {   
        alert('Error!'); 
    }   
}); 

7. Delete the active state (jQuery Mobile) from the link list view

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

8. jQuery Mobile disable the default style from the drop-down selection (jQuery Mobile)

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

9. dynamically updated list view (jQuery mobile)

var output  = '<li><img src="' + item.image + '" alt="' + item.title + '" />';   
output += '<h3><a href="' + item.url + '">' + item.title + '</a></h3>';   
output += '</li>';     
   
$('#mylistul').append(output).listview('refresh'); 

10. dynamically add input and applied default style sheet (jQuery Mobile)

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

 

Reproduced in: https: //www.cnblogs.com/JoannaQ/archive/2013/02/21/2919913.html

Guess you like

Origin blog.csdn.net/weixin_34006965/article/details/94153676