When developing mobile websites, some Javascript Code It is often used. The following are 10 common JavaScript codes. Note that several pieces of code must be supported by jquery mobile framework.
1. If the webpage is viewed on the iPhone or android browser, add the "iPhone" or "android" class name to the subject element.
JavaScript code
- If (navigator. useragent. Match (/iPhone/I )){
- $ ('Body'). addclass ('iphone ');
- }Else if (navigator. useragent. Match (/Android/I )){
- $ ('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
JavaScript code
- Window. scrollto (0, 1 );
3. Prevent webpage touch scrolling
JavaScript code
- Notouchmove = function (event ){
- Event. preventdefault ();
- }
- <Div data-role ="Page" id = "home" ontouchmove = "notouchmove (event);">
- ...
- </Div>
4. Information displayed during horizontal browsing
JavaScript code
-
- 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 );
5. Partial description information is displayed. complete information is displayed when you click.
JavaScript code
-
- 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. Redirect to another page (jquery mobile) when receiving a successful Ajax request)
JavaScript code
-
- 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 activity status (jquery mobile) from the link in the List View)
JavaScript code
- $ ('Div '). Live ('pageshow', function (event, UI ){
- $ ('[Data-role = listview] Lil'). removeclass ("UI-BTN-active ");
- });
8. Disable the default jquery mobile style from the drop-down list)
JavaScript code
- $ (Document). BIND ("mobileinit", function (){
- $. Mobile. Page. Prototype. Options. keepnative ="Select ";
- });
9. Dynamic Update List View (jquery mobile)
JavaScript code
- VaR output = '<li> ';
- Output + ='<H3> <a href = "' + item. url + '">' + item. Title + '</a>
- Output + ='</LI> ';
- $ ('# Mylistul'). append (output). listview ('refresh ');
10. dynamically Add the form input and default apply style (jquery mobile)
JavaScript code
- VaR html = '<input type = "Search" name = "suburb" id = "suburb" Placeholder = "Enter suburb"/> ';
- $ ('# Searchform'). append (HTML );
- $ ('# Suburb'). textinput ();
Recommended books
The following two books on HTML5 and jquery will help you understand and use the jquery mobile framework.
HTML5: Up and running
Jquery: novice to ninja