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?

user717236
  • 4,799
  • 19
  • 61
  • 98

1 Answers1

1

My recommendation would be to check beforehand if the user is using the app and then modify the behavior based on that, rather than checking if a new window opened. I have in the past accomplished this by checking the screen.width property (can be similarly accomplished in CSS), but you can do it more cleanly (courtesy of this post's answer):

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

This is cleaner, as you can determine at load time whether you'll need to open in the current window or a new window, rather than reacting later to something that happens for the user.

Community
  • 1
  • 1
vaindil
  • 6,681
  • 18
  • 61
  • 105
  • I think it's a good idea. I agree with you that it's cleaner, but this doesn't detect whether the user is inside the app or not, just if he or she is using a mobile device. If the user is using Chrome on iPhone, for example, it should open a new window. If it's in the app, however, it'll open in the same window. – user717236 Sep 12 '13 at 18:04
  • 1
    Without being able to detect whether someone is in the app via other methods (e.g. modifying the app to post data to the webpage), there's no way to detect whether the app itself is open. Javascript also does not allow any interactions outside of the current window for security reasons, so you'd only be able to find a workaround, possibly using `onblur`, but they would be crude methods at best. – vaindil Sep 12 '13 at 18:26
  • Thank you. I'm willing to use crude methods, until the app developer releases a new version (I spoke with him and he has disabled popups and may consider enabling them in the next version). What was the method you spoke of that would use onblur? – user717236 Sep 13 '13 at 16:24