3

I want to open a link using 'href' (not using javascript/jQuery), in a new tab in the same window. If the link is already opened it should just reload it and focus it.

How can i code to achieve this?

I tried.

<a href="name.html" target="_blank">Click</a>

It doesn't reload the page if it already exists. It opens the link in new tab again and again.

Any Suggestion?

leo
  • 8,106
  • 7
  • 48
  • 80
user3784251
  • 520
  • 2
  • 10
  • 24

2 Answers2

6

Simply give your target a unique value, like this:

<a href="http://stackoverflow.com" target="mynewwindow">Always opens in the same window</a>

This is the default behaviour of the target attribute. _blank is one of an handful special keywords, with special behaviours, as described in the HTML standard.

Note that the standard doesn't talk about windows, but about browsing context. A browsing context is typically a window or a tab, but this is up the browser.

As for the special keywords:

  • _blank means always use a new browsing context (e.g. tab or window)
  • _self means always use the same browsing context
  • _parent means use a parent context, if any
  • _top means use the topmost context, if there are any parents

With iframes, things are a little bit more complex.

edit: Note also that the second time a user clicks the link, the target page will not be reloaded. If you need a refresh, you will have to use Javascript.

leo
  • 8,106
  • 7
  • 48
  • 80
0

If you want to always keep a popup "focused" you can use a trick originally found here. By rapidly open/close/open it we ensure it's always behaves like new window and grabs focus.

<a href="http://stackoverflow.com" onclick="
if(window.open('','reuse_win') != null) {
   window.open('','reuse_win').close();
}" target="reuse_win">Always opens in the same window and FOCUS</a>

Also works when submitting a "form" to a targeted window.

<form name=f1 method=post action=test.html>

  <input type=text name=name value='123'>

  <input type='submit' value='Submit' 

  onclick="if(window.open('','reuse_win') != null) {
       window.open('','reuse_win').close();
    }; this.form.target='reuse_win'; return true;">

</form>
boateng
  • 910
  • 11
  • 21