1.先上一段 html 代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
</head>
<body>
<h1>这是一段内容</h1>
<input type="button" value="测试" onclick="check()" />
<input type="button" value="测试2" onclick="check2()"/>
</body>
</html>
2. js 代码
function loadURL(url) {
var iFrame;
iFrame = document.createElement("iframe");
iFrame.setAttribute("src", url);
iFrame.setAttribute("style", "display:none;");
iFrame.setAttribute("height", "0px");
iFrame.setAttribute("width", "0px");
iFrame.setAttribute("frameborder", "0");
document.body.appendChild(iFrame);
// 发起请求后这个iFrame就没用了,所以把它从dom上移除掉
iFrame.parentNode.removeChild(iFrame);
iFrame = null;
}
function check() {
loadURL("lichongyang:abc");
}
function check2() {
loadURL("lichongyang2:abc");
}
3.当js 调用 iOS 的时候 通过上面的js 代码 会重定向一个 请求的url 在
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL * url = [request URL];
if ([[url absoluteString] rangeOfString:@"abc"].location!= NSNotFound) {
UIAlertView * alertView = [[[UIAlertView alloc] initWithTitle:@"test" message:[url absoluteString] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alertView show];
return NO;
}
//获取当前页面的title
//self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
return YES;
}