WKWebView not opening SOME target=“_blank” links

前端 未结 1 910
臣服心动
臣服心动 2020-12-21 09:23

I have implemented the accepted solutions here and it does work for some websites. For eg: Go to www.tomcruise.com and click on his trailers. Each of those links have targe

相关标签:
1条回答
  • 2020-12-21 09:40

    I was hoping that I could solve it using the WKWebView delegate methods, but I could not figure it out.

    So I went to the UIWebView era's solution of running a javascript function upon completion of web Page loading

    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
        let jsCode = "var allLinks = document.getElementsByTagName('a');if (allLinks) { var i;for (i=0; i<allLinks.length; i++) {var link = allLinks[i];var target = link.getAttribute('target');if (target && target == '_blank') {link.setAttribute('target','_self');} } }"
        webView.evaluateJavaScript(jsCode, completionHandler: nil)
    }
    

    This fixed the issue where tapping on the links in any google plus Posts page was resulting in an empty page being loaded


    UPDATE on Nov 3rd 2015: The phenomenon explained in the question, no longer happens for me in Swift 2.0 code. So , you should be able to use the solution presented here for all your purposes

    0 讨论(0)
提交回复
热议问题