David Woolley
David Woolley

Reputation: 73

HTML links with target

I asked this previously in another place and got no useful replies.

One of the possible uses of the "target" attribute on an HTML link is to specify a named window, like:

    <a href="somepage.html" target="mySpecialWindow">Click here</a>

Presumably the reason for naming a target, as opposed to just using "_blank", is that you want to be able to reference that SAME window for other links. For example, say you have a main page that you want to always remain in view, which has links to several help pages, and you want all of those help pages to open in a specific secondary window. So clicking the first help link opens the secondary window, clicking a second help link replaces the contents of the secondary window with a different help page, clicking the third help link replaces the contents of that secondary window again, etc.

But the existing browsers (Firefox, Chrome, etc.) do not do this. If you use a target attribute on your links with a specific (identical) window name, clicking those links opens a new, separate window with each click, even though the target name is the same. In other words, it behaves exactly as if you used target="_blank".

Why is this? What is the point of having the ability to name target windows if naming a window acts exactly the same as using target="_blank" ?

And is there any way to make a link actually use an existing window that has been opened with the same name instead of opening yet another window?

Upvotes: 3

Views: 1728

Answers (3)

Don Rhummy
Don Rhummy

Reputation: 25830

Have you tried it using Javascript?

//You keep a reference to the window
var mySpecialWindow = undefined;

function openInSameWindow(url)
{
    //First time opening
    if ( typeof( mySpecialWindow ) === "undefined" )
    {
        mySpecialWindow = window.open(
            url,
            "mySpecialWindow",
            "width=300, height=250"
        );
    }

    //Use existing popup window/tab
    else mySpecialWindow.location.href = url;

    return false;
}

//html
<a href="#" onclick="openInSameWindow('http://someurl.com')">first link</a>
<a href="#" onclick="openInSameWindow('http://someotherurl.com')">second link</a>

Upvotes: 1

c-smile
c-smile

Reputation: 27460

Attribute "target" allows to load documents into particular frame/iframe on the page. It is far from windows in these "tab days" but rather about views - containers of [sub]documents.

Upvotes: 0

zajd
zajd

Reputation: 761

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a

This attribute specifies where to display the linked resource. In HTML4, this is the name of, or a keyword for, a frame. In HTML5, it is a name of, or keyword for, a browsing context (for example, tab, window, or inline frame). The following keywords have special meanings:

  • _self: Load the response into the same HTML4 frame (or HTML5 browsing context) as the current one. This value is the default if the attribute is not specified.
  • _blank: Load the response into a new unnamed HTML4 window or HTML5 browsing context.
  • _parent: Load the response into the HTML4 frameset parent of the current frame or HTML5 parent browsing context of the current one. If there is no parent, this option behaves the same way as _self.
  • _top: In HTML4: Load the response into the full, original window, canceling all other frames. In HTML5: Load the response into the top-level browsing context (that is, the browsing context that is an ancestor of the current one, and has no parent). If there is no parent, this option behaves the same way as _self.

Upvotes: 0

Related Questions