Question

I have a table data in a form and there is dynamic link in each row which opens an external link in a new window (new browser tab). These rows are in n numbers with dynamic id.

What I need here is,when user try to submit the table details, they must close all the child windows. If any child div is still opened and if he tries to submit the form, it should given alert to close the child windows

Here is the sample DEMO of the form

<form>
    <table border="1">
        <tr>
            <td><input type="text" /></td>
            <td>SC1</td>
            <td><a href="http://www.google.com" target="_blank">this is an external link</a></td>

<tr>
            <td><input type="text" /></td>
            <td>SC2</td>
            <td><a href="http://www.google.com" target="_blank">this is an external link</a></td>

            <tr>
            <td><input type="text" /></td>
            <td>SC3</td>
            <td><a href="http://www.google.com" target="_blank">this is an external link</a></td>
</tr>
    </table>
            <input type="submit"  value="submit" />
</form>
Was it helpful?

Solution

You can store each opened window in an array and perform a check for open windows when the user tries to submit the form:

var windows = [];
$('a').click(function () {
    var myWindow = window.open(this.href);
    windows.push(myWindow);
});

$('form').submit(function () {
    for (var i = 0; i < windows.length; i++) {
        if (!windows[i].closed) {
            alert('Close all windows before continuing');
            return false;
        }
    }
});

Demo fiddle

OTHER TIPS

looks like you need to keep track of open windows and close them on submit

var windows = [];
$('a').click(function(){
    var win = window.open($(this).attr('href'),"_blank");
    windows.push(win);
});
$('input[type="submit"]').click(function(){
    if(windows && windows.length > 0)
        $.each(windows,function(index,el){
            el.close();
        });
});

here is the fiddle http://jsfiddle.net/JpDhX/4/ you can use

1.Instead of <a href> use onclick javascript:window.open.
2.Give title to the window.
3.For closing
  if (document.title === "the title you want") {
    window.close();
  }
4.Give the above code in button Click.

Have a js function call window.open() to create the new windows . Keep track of the references created.

Also , the code as you provided in the demo doesn't guarantee the links are opened in new windows.

relevant fiddle http://jsfiddle.net/DS83p/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top