How to tell the difference between an iframe loadi

2019-03-12 23:18发布

问题:

In the [webView:shouldStartLoadWithRequest:navigationType:] event, how can you tell the difference between an iframe that's loading content vs. the entire page loading content? It seems like the both call this same event with the same arguments, the only difference being the URL that's going to be loaded, so there is no way to tell the difference between the two.

I've thought of a few ways to tell the difference though:

  • Parse the source code of the page and store a list of all the iframe src attributes, assume that if the url loading is one of those in my list it's an iframe. Of course this assumption might be incorrect if it actually does navigate to the page.
  • Same as above, but run some JavaScript on the page and to get the iframe src attributes.
  • Using JavaScript, set up hooks for whenever a user tries to navigate to a page, similar to http://niw.at/articles/2009/02/06/how-to-enable-the-popup-window-on-uiwebview/en
  • Using JavaScript, set up hooks right before an iframe tries loading data (not sure how I would do something like this).
  • Modify each iframe's src attribute to include a special string at the end of it's src attribute e.g. "#iframe-loading". Load the UIWebView with this modified source code. Although this will only work for iframes that existed on the original page as it was queried the first time, not those that are loaded dynamically, e.g. via JavaScript.
  • Let the page load normally by returning YES in the [webView:shouldStartLoadWithRequest:navigationType:] event, and once it's done loading, see if the WebView's URL changed or not... if it did that means it was a page redirect if not it probably means it was an iframe that was loaded.
  • Using JavaScript, override the window.location property setter to run my own code before it will actually change the window's location. I could then communicate with Objective-C to let it know that the next load is actually going to be a redirect. Is this the only way to redirect in JavaScript though?

What's the best way to do this? Can you think of any ways I haven't thought of? Do iframes have a special event/property I could manipulate with JavaScript to help me out?

Thanks

Update: It's not as simple as just checking the navigationType. Although clicking a link to open up a new page will show up as navigationType = 0, a JavaScript redirect (changing window.location) will show up as navigationType = 5. An iframe load also shows up as navigationType = 5. So when navigationType = 5, you don't know if the entire page changed URLs via JavaScript or if it is simply an iframe loading on the same page.

回答1:

 (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

BOOL isFrame = ![[[request URL] absoluteString] isEqualToString:[[request mainDocumentURL] absoluteString]];

}


回答2:

I just used this method:

Let the page load normally by returning YES in the [webView:shouldStartLoadWithRequest:navigationType:] event, and once it's done loading, see if the WebView's URL changed or not... if it did that means it was a page redirect if not it probably means it was an iframe that was loaded.



回答3:

I've successfully used the HTTP "referer" (the actual header is misspelled) field to detect this. This should either be the URL of the main page (available from the webview) or something else -- probably a frame.

NSString *referer = [request.allHTTPHeaderFields objectForKey:@"Referer"];
NSString *currentPage = [webView.request.mainDocumentURL absoluteString];
BOOL isFrameLoad = [referer isEqualToString:currentPage] == NO;