<form target=“_blank”> not working in iOS 7.0.3

Following code works perfectly in iOS 6.0.1 (using iOS virtual keyboard, I pressed "Go" button on input box)

<html>
    <body>
        <form action="http://stackoverflow.com/" target="_blank">
            <input type="text" />       
        </form>
    </body>
</html>

But when I tried the exact same code on iOS 7.0.3, this does not work at all. No response after I press "Go" button on iOS keyboard.

If I remove [target="_blank"] from form tag, then it works properly.

I have no idea what is the reason for this problem. Does anyone have the same issue?

Answers:

Answer

Popups are blocked by safari iOS by default.

Disable the block feature by settings -> Safari -> Block Pop-ups.

Answer

Assuming the HTML is outside of your control, a workaround is to remove the form target property programmatically. Implement the following in your web view delegate:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
        NSString *script = @"for (i=0; i<document.forms.length; i++) { document.forms[i].target = null; }";
        [aWebView stringByEvaluatingJavaScriptFromString:script];
    }
}
Answer

Here is a little jQuery workaround I used to remove the target="_blank" attribute on iOS only, on submit:

$('#searchform').submit(function(e) {
  if(navigator.userAgent.match(/(iPod|iPhone|iPad)/i)) {
    $(this).removeAttr('target');
  }
});

It seems there isn't any better solution as the solution to transit through a temporary a tag doesn't work on iOS 8+.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.