0

I have a page which contains a form. The form submits via a Javascript call, and it opens a new window via the target set to "_blank".

<div id="ajax_response_html">
    <form name="testform_name" id="testform_id" method="post" target="_blank" action="index.jsp" accept-charset="UTF-8" />
        <input type="hidden" name="textbox_name" id="textbox_id" value="test" />
    </form>
</div>

Upon an onclick event, the form is set inside the above div using an ajax call. At that point, I submit the form in JavaScript.

 function setResponse() {
     var response = http.responseText;
     document.getElementById("ajax_response_html").innerHTML = response;
     document.getElementById("testform_id").submit(); 
 }

A new window opens and works just fine. However, I have a mobile app that uses a webview -- a browser session inside the app -- and it does not open a new window. I have no control, at the moment, over the app and no access to its source code. My only recourse is to modify the page to appease both app and non-app users. For the former, it'll open up in the same window. For the latter, it'll open up a new window. That's the plan.

My thought process is to detect if a new window has opened. If not, then I open it up in the same window.

if (!windowOpen) {
    // Open page in the same window
}

How can I detect if a new window has opened after submitting the form?

Thank you.

Update

I spoke to the developer of the app, who said he has popups disabled and will not changes any time soon.

I modified the page such that instead of calling the html form file, via ajax, I open this same file using the JavaScript window.open function:

function openForm() {
    var formWindow = window.open('FormFile.html', '_blank', 'height=' + screen.height + ', width=' + screen.width');
}

Within FormFile.html, I attached an onload event that submits the form, except the target is set to "_self", not "_blank".

Now, it opens in the same window within the app and opens a new window everywhere else. My question is, since popups are disabled within the app, why would window.open work, yet the original method I was using to submit the form was failing?

4

1 回答 1

1

我的建议是事先检查用户是否正在使用该应用程序,然后根据此修改行为,而不是检查是否打开了新窗口。我过去通过检查screen.width属性来完成这一点(可以在 CSS 中类似地完成),但你可以更干净地做到这一点(由这篇文章的回答提供):

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    // some code..
}

这更简洁,因为您可以在加载时确定是否需要在当前窗口或新窗口中打开,而不是稍后对用户发生的事情做出反应。

于 2013-09-12T17:56:51.217 回答