how to handle base tag target attribute in iphone uiwebview to open new window

Posted by user217428 on Stack Overflow See other posts from Stack Overflow or by user217428
Published on 2010-04-07T17:08:45Z Indexed on 2010/04/07 17:13 UTC
Read the original article Hit count: 491

Filed under:
|
|

When the links are supposed to open a new window, iphone uiwebview won't trigger an event when user click these links. We had to use javascript to do some trick to the target attribute of the links.

I can handle 'a' tag to open in the '_self' window with the trick without problem. But when I do it the same way with the 'base' tag. it doesn't work.

I believe the base target is set by the javascript. But the base tag is in the head, which may be handled by the uiwebview before my javascript executed, so the target change may not reflected in the webkit engine.

Could someone please give some suggestion, so I can open the link in the same uiwebview?

The following is the sample HTML opened in the uiwebview

<html>
<head>
<base target='_blank'>
</head>
<body>
<a href='http://google.ca'>google</a>
</body>
</html> 

The following is the code to be executed in the

(void) webViewDidFinishLoad: (UIWebView*)webView

static NSString* js = @""
    "function bkModifyBaseTargets()"
     "{"
            "var allBases = window.document.getElementsByTagName('base');"
            "if (allBases)"
            "{"
                "for (var i = 0; i < allBases.length; i++)"
                "{"
                    "base = allBases[i];"
                    "target = base.getAttribute('target');"
                    "if (target)"
                    "{"
                        "base.setAttribute('target', '_self');"
                    "}"
                "}"
            "}"
    "}";
        [webView stringByEvaluatingJavaScriptFromString: js];
    [webView stringByEvaluatingJavaScriptFromString: @"bkModifyBaseTargets()"];

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiwebview