UIWebView检测javascript何时加载新页面

UIWebView检测javascript何时加载新页面

本文介绍了UIWebView检测javascript何时加载新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIWebView,我想要在新页面上打开所有链接。

I have a UIWebView and I want ALL links to open on a new page.

我有这段代码来检测用户何时点击链接并在新页面上打开该链接:

I have this code to detect when a user clicks a link and open that link on a new page:

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


//If the user clicked a link don't load it in this webview
if (navigationType == UIWebViewNavigationTypeLinkClicked) {


        NSURL* URLToGoTo = [request URL];
        self.fullWebView.url = URLToGoTo;
        [self.navigationController pushViewController:fullWebView animated:YES];
        return NO;
}
//Else this is the webview being loaded for the first time, let it load.
return YES;

问题是某些网站使用javascript打开这样的链接:

The problem is some website use javascript to open links like this:

win = window.open("/magic/card.asp?name="+cardname+"&set="+set+"&border="+border, windowName, params);

if (!win.opener)
{
    win.opener = window;
}

不幸的是,这些类型的链接没有UIWebViewNavigationTypeLinkClicked属性,并且会在我的UIWebView的同一个窗口。

Unfortunately these types of links do not have the UIWebViewNavigationTypeLinkClicked property and will open in the same window of my UIWebView.

我试着查看URL的scheme属性,看看它是否是javascript,但它看起来与常规链接使用的URL相同。

I tried looking at the scheme property of the URL to see if it were "javascript" but it looks identical to the URL used by regular links.

有人能想到一种方法来检测javascript函数打开网页的时间吗?

Can anyone think of a way to detect when a webpage is being opened by a javascript function?

我想最糟糕的情况我可以使用布尔值来确定这是否是第一次加载我的UIWebView并加载新页面中的所有后续链接,但是必须有更好的解决方案

I suppose worst case scenario I can use a boolean to determine if this is the first time the my UIWebView is being loaded and load all subsequent links in a new page, but there must be a better solution

谢谢!

推荐答案

是:要捕获Javascript引起的页面加载,您应该在 webView:shouldStartLoadWithRequest:navigationType:中检查 navigationType == UIWebViewNavigationTypeOther

Yes: to catch the Javascript-induced page loads you should check for navigationType == UIWebViewNavigationTypeOther in webView: shouldStartLoadWithRequest:navigationType:.

这篇关于UIWebView检测javascript何时加载新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:11