tags:

views:

93

answers:

1

Hello,

I have a tab open in my browser which runs my web site. Upon clicking on a button, my site is going to open other windows or tabs with different URLs in each.

<form action="search.php" method="get">
    <input type="text" name="item" size="30" style="font-size:140%" id="item">
    <input type="submit" value="Search All" style="font-size:140%" id="submit">
</form>

The JS that performs this is provided below:

<script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

<script>

$(document).ready(function(){
    function get(id) {
        return document.getElementById(id);
    }
    $("#submit").click(function() {
        get("guitarcentertext").value = get("item").value;
        get("music123text").value = get("item").value;
        get("musiciansfriendtext").value = get("item").value;
        get("prosoundtext").value = get("item").value;

        $("input#guitarcenterbutton").click();
        $("input#btnSearch").click();
        $("input#musiciansfriendbutton").click();
        $("input#prosoundbutton").click();
        return false;
    });
});
</script>

Question: Is there a way that upon the click of another button, I can close the tabs that I previously opened?

Thanks for your help! :)

A: 

You can use window.open() to create new windows, and to control when to close them.

Example:

function mypopup() {
    mywindow = window.open ("http://www.javascript-coder.com",
                            "mywindow",
                            "location=1,status=1,scrollbars=1,"+
                            "width=100,height=100");
    mywindow.moveTo(0,0); // move the window to a particular location

    // do your stuff ...

    mywindow.close(); // close that popup when you are done
}

As you can see, you can control when you want to close the windows using window.close().

You can have links open in new tabs simply by using target="_blank" in the link, but you say that you have a form whose submission will cause a new window to be opened. But instead of using a submit button, use a plain button and attach an event handler to it being clicked. When it is clicked, open your window and work your mojo.

Regarding your query about opening tabs instead of windows, I'm not entirely sure it can be done. I can think of a couple of bad ways to do it using server side participation, but I'm pretty sure you are not looking for that. Even using target="_blank" just tells the browser not to open the link in the same page. Its upto the browser to choose a new window or a new tab (mostly, its a new tab).

Cheers!

Here Be Wolves