UIWebView获取正在加载的URL和网页title
UIWebView获取正在加载的URL和网页title
Ios基础语法 UIWebView
### UIWebView获取正在加载的URL和网页title
project中遇到个问题,其中解决这个问题最主要的地方就是如何获取用户点击UIWebView中的一个超链接。
经google后得知,其实是可以用JSP与object-c交互获取的。
方法是很多的。这里是我找的一种。
这里我可以获取当前页面的URL。所以我们可以让用户进入超链接后再获取URL。
NSURLRequest 有一个方法 就是 URL ,返回它的url地址。所以如果要知道当前页面的地址,可以[webView.request URL]
再加上UIWebView的delegate中的一个方法webViewDidFinishLoad,我们就可以再每次页面加载完成后获取页面的url 地址。
如果要在进入超链接前获取将要进入超链接的地址,可以使用delegate中的
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
方法。
NSString *currentURL= myWebView.request.URL.absoluteString;
NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@”document.title”];
|获取所有html:NSString *lJs = @”document.documentElement.innerHTML”;
获取网页title:NSString *lJs2 = @”document.title”;
UIWebView *lWebView = [self getCurrentWebView];
NSString *lHtml1 = [lWebView stringByEvaluatingJavaScriptFromString:lJs];
NSString *lHtml2 = [lWebView stringByEvaluatingJavaScriptFromString:lJs2];
JavaScript获取网页信息总结
|
|—————————————————————————————————————————————————————————————————————————————————————————————————————————————————-|
JavaScript获取当前页面URL、title等,具体怎么用就看自己了~
由于本站用了伪静态,所以获取不到文档名.document.location.port;是获取URL关联的端口号码,thisHash = document.location.hash;是获取链接属性中在井号“#”后面的分段。
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;//获取网页内容文字
thisBodyText = document.body.innerText;//获取网页内容文字

