- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { //isNotFirstLoad' marks if it is the fisrt load.
//isNotFirstLoad是代表當前這個webview是不是已經加載了一次request if (isNotFirstLoad) { //如果不加上這個判斷,后面出現的loadRequest函數的調用就會導致出現死循環 //create an new webview CGRect frame = _myWebView.frame; [_myWebView removeFromSuperview]; _myWebView = [[EasyJSWebView alloc] initWithFrame:frame]; [self.view addSubview:_myWebView]; //init the new webview _webView.delegate = self; javaScriptInterface* interface = [javaScriptInterface new]; [self.webView addJavascriptInterfaces:interface WithName:@"interface"]; [_myWebView loadRequest:request]; //reset the firstload flag to load the new request isNotFirstLoad = NO; //返回No,則不加載本方法傳入的的request return NO; } isNotFirstLoad = YES; return YES; //YES if the web view should begin loading content; otherwise, NO .
}