Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html open a url on new target and focus

I am trying to fix a web site.

It opens a help page in a new window/tab via <a href="..." target="help"> (no other frame has this name)

This works well the first time opening a new window/tab, for the help. But on subsequent clicks the window/tab is loaded but remains hidden.

I tried this:

<script>
    function OpenAndFocusHelp() {
        win=window.open('help/1000CH00017.htm','help');
        win.focus();
    }      
</script>

<a href="help.html" target="help" 
   onclick="OpenAndFocusHelp()">Help</a>

It did not work!

like image 892
ctrl-alt-delor Avatar asked Dec 28 '10 13:12

ctrl-alt-delor


People also ask

How do I open a link in a new tab in target?

You can use the target="_blank" attribute if you want your users to click on a link that opens up a new browser tab. The target="_blank" attribute is used inside the opening anchor tag like this.

How do you make a link open in a new tab HTML?

You can make a HTML link open in a new tab by adding the target=”_blank” attribute. You should insert this after the link address.

How do I make a link open in a new tab instead of a new window?

Simply press and hold the Ctrl key (Cmd on a Mac) and then click the link in your browser. The link will open in a new tab in the background.

How can you open a link in a new tab or browser window in HTML?

You just need an anchor ( <a> ) element with three important attributes: The href attribute set to the URL of the page you want to link to. The target attribute set to _blank , which tells the browser to open the link in a new tab/window, depending on the browser's settings.


2 Answers

It seems that modern browsers do not allow you to window.focus an existing window. Or at least, it will not give that window focus. (IE9 will actually blink the tab, but most other browsers merely load it but give no indication that the user's attention should be drawn to the new window.)

Therefore, one solution that presents itself is to close the window first, and then re-open it immediately after. For example, declare the following function:

window.openOrFocus = function(url, name) {
    if (!window.popups)
        window.popups = {};
    if (window.popups[name])
        window.popups[name].close();
    window.popups[name] = window.open(url, name);
}

Now, you can write HTML such as this:

<a href="javascript:void(0);" onclick="openOrFocus('http://jsfiddle.net/k3t6x/2/', 'window1')">Window 1</a><br />
<a href="javascript:void(0);" onclick="openOrFocus('http://jsfiddle.net/KR6w3/1/', 'window2')">Window 2</a><br />

Since it closes the window first, it reliably gives focus to the newly created child window.

This solution also uses the window.popups namespace, so rename the usages of that in the javascript sample if you have a function named popups or have otherwise collided with it.

Caveat: This does not work after a post-back. This is because once you navigate away from the current page, it is no longer the owner of the child windows. Therefore, it can no longer close them. However, it merely degrades to the usual (non-focusing) behavior of using the target attribute.

Tested In: Firefox 4, Chrome 11, IE 9
JsFiddle Demo: http://jsfiddle.net/aqxBy/7/

like image 171
Kirk Woll Avatar answered Sep 19 '22 23:09

Kirk Woll


I think this feature is browser-specific and you can't define the behavior for focusing new tabs or windows..

like image 24
bevacqua Avatar answered Sep 21 '22 23:09

bevacqua