views:

348

answers:

1

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'&gt;google&lt;/a&gt;
</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()"];
A: 

Sorry, Actually this code works for base tag, too.

This code is not my original code. There is a javascript error somewhere before this code executed, so it is not executed yet.