HTML links with target

I asked about this earlier elsewhere and got no helpful answers.

One possible use of the "target" attribute for an HTML link is to specify a named window, for example:

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

      

Presumably the reason for naming the target, rather than just using "_blank", is because you want to be able to link to that MOST window for other links. For example, suppose you have a home page that you want to keep in sight at all times, that has links to multiple help pages, and you want all of the help pages to open in a separate sub-window. Thus, clicking the first help link opens the secondary window, clicking the second help link replaces the contents of the secondary window with another help page, clicking the third help link again replaces the contents of that secondary window, and so on.

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

Why is this? What's the point of being able to specify target windows if the window name acts exactly the same as using target = "_blank"?

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

+3


source to share


3 answers


Have you tried it with 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>

      

+1


source


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 or keyword for a frame. In HTML5, this is a name or keyword for a viewing context (such as a tab, window, or inline frame). The following keywords have special meanings:



  • _self: Load the response into the same HTML4 frame (or HTML5 viewing 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 parent HTML4 frameset of the current calendar or the original HTML5 browser context of the current one. If there is no parent, this parameter behaves the same as itself.
  • _top: In HTML4: Load the response into a full source window, canceling all other frames. In HTML5: Load the response into a top-level browsing context (that is, a browsing context that is an ancestor of the current one and has no parent). If there is no parent, this parameter behaves the same as itself.
0


source


The "target" attribute allows you to load documents to a specific frame / iframe on the page. This is a far cry from windows in these "tab days", but rather views - document containers [sub].

0


source







All Articles