Web page JS

Source: Internet
Author: User

JS and iOS communication, the main use of two methods: (PhoneGap framework is based on this principle)

1, the Stringbyevaluatingjavascriptfromstring method of UIWebView

2, the Uiwebviewdelegate

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype :(Uiwebviewnavigationtype) Navigationtype method

Example:

The upper part is a UIWebView that implements Uiwebviewdelegate

    1. -(void) viewdidload
    2. {
    3. [Super Viewdidload];
    4. NSString *path = [[NSBundle mainbundle] pathforresource:@ "Jm/info" oftype:@ "html"];
    5. Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl Fileurlwithpath:path];
    6. [Self.webview Loadrequest:request];
    7. }


  1. #pragma mark-uiwebviewdelegate
  2. -(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype
  3. {
  4. if ([Request.mainDocumentURL.relativePath isequaltostring:@ "/getinfo/name"])
  5. {
  6. NSString *info = [[Uidevice currentdevice] name];
  7. NSString *js = [NSString stringwithformat:@ "Showinfo (\" name\ ", \"%@\ ")", info ";
  8. [Self.webview Stringbyevaluatingjavascriptfromstring:js];
  9. return false;
  10. }
  11. if ([Request.mainDocumentURL.relativePath isequaltostring:@ "/getinfo/systemversion"])
  12. {
  13. NSString *info = [[Uidevice currentdevice] systemversion];
  14. NSString *js = [NSString stringwithformat:@ "Showinfo (\" systemversion\ ", \"%@\ ")", info ";
  15. [Self.webview Stringbyevaluatingjavascriptfromstring:js];
  16. return false;
  17. }
  18. return true;
  19. }


JS Code:

    1. <! DOCTYPE HTML>
    2. <html>
    3. <head>
    4. <title>city</title>
    5. <Meta charset="Utf-8">
    6. <Meta name="viewport" content="width=device-width, initial-scale=1">
    7. <link rel= "stylesheet" href="jquery.mobile-1.0.css"/>
    8. <script type="Text/javascript" src="Jquery.js"></script>
    9. <script type="Text/javascript" src="Jquery.mobile-1.0.js"></ Script>
    10. <script>
    11. function GetInfo (name)
    12. {
    13. window.location = "/getinfo/" +name;
    14. }
    15. function Showinfo (id,info)
    16. {
    17. $ ("p#" +id). HTML (info);
    18. }
    19. </Script>
    20. </head>
    21. <body>
    22. <div data-role="page">
    23. <div data-role="Content">
    24. <h2>divice Info</h2>
    25. <div data-role="Collapsible-set" data-theme="C" data-content-theme="D">
    26. <div data-role="collapsible">
    27. <h3 onclick="getInfo (' name ')">name</h3>
    28. <p id="name"></P>
    29. </div>
    30. <div data-role="collapsible">
    31. <h3 onclick="getInfo (' systemversion ')">systemversion</h3>
    32. <p id="systemversion"></P>
    33. </div>
    34. </div>
    35. </div>
    36. </div>
    37. </body>
    38. </html>

Web page JS

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.