user717236
user717236

Reputation: 5039

How do I detect if a new window has been opened? The window opens via a submit with target="_blank"

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?

Upvotes: 0

Views: 2064

Answers (1)

vaindil
vaindil

Reputation: 7844

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.

Upvotes: 1

Related Questions