stringByEvaluatingJavaScriptFromString 的固定用法

获取页面的 url:

[webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];

获取页面的 title:

[webview stringByEvaluatingJavaScriptFromString:@"document.title"];

获取页面的 HTML 内容:

[webview stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];

获取页面的 body 之间的 HTML 内容:

[webview stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

向页面中插入 Javascript 代码并执行:

 [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"  
  "script.type = 'text/javascript';"  
 "script.text = \"function myFunction() { "  
  "var field = document.getElementsByName('q')[0];"  
 "field.value='xxxxxx';"  
  "document.forms[0].submit();"  
  "}\";"  
 "document.getElementsByTagName('head')[0].appendChild(script);"];  
 [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];

你可能感兴趣的:(stringByEvaluatingJavaScriptFromString 的固定用法)