利用JavaScript從UIWebView獲取疾就、修改缕碎、提交網(wǎng)頁(yè)內(nèi)數(shù)據(jù)的方法
在UIWebView的內(nèi)容加載完之后,可以利用javascript獲取其頁(yè)面內(nèi)的數(shù)據(jù)秧饮,核心就是通過(guò)UIWebView的方法:
NSString *string = [webView stringByEvaluatingJavaScriptFromString:@"此處放js代碼" ];
以下是一些常用的js腳本:
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
thisTitle = document.title;
thisProtocol = document.location.protocol;
thisPort = document.location.port;
thisHash = document.location.hash;
thisSearch = document.location.search;
thisPathname = document.location.pathname;
thisHtml = document.documentElement.innerHTML;
thisBodyText = document.documentElement.innerText;//獲取網(wǎng)頁(yè)內(nèi)容文字thisBodyText = document.body.innerText;//獲取網(wǎng)頁(yè)內(nèi)容文字
也可以通過(guò)同樣的方法去設(shè)置頁(yè)面內(nèi)容(比如幫用戶輸入表單數(shù)據(jù))比如:
NSString *string = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('field_2').value='a value';" ];
就可以修改field_2的值了
同樣也可以去模擬頁(yè)面內(nèi)按鈕的點(diǎn)擊阴颖,提交頁(yè)面峦甩,比如:document.getElementById('aButtonName').click();
或者嘱朽,假設(shè)知道按鈕是第幾個(gè)input標(biāo)簽(假設(shè)為第一個(gè))document.getElementsByTagName('input').item(0).click();
也可以設(shè)置checkBox的狀態(tài):document.getElementById('aCheckBoxId').checked=true;
參考 :
[http://hi.baidu.com/zfpp25/blog/item/0bc5e3565a7e632e0cf3e3d7.html]
(http://hi.baidu.com/zfpp25/blog/item/0bc5e3565a7e632e0cf3e3d7.html)
[http://www.cnblogs.com/del/archive/2009/01/07/1370907.html]
(http://www.cnblogs.com/del/archive/2009/01/07/1370907.html)
http://blog.csdn.net/studyrecord/article/details/6213843