Reputation: 314
I have a data grid with a hyperlink control that users can click and it will open a new tab displaying information about the selected item. My issue is that if the same item is clicked 3 times I get 3 new tabs on my browser.
Is there a way open the tab the first time the link is clicked and as long as it is open and someone selects the link can it put the focus on the already created tab instead of opening a new one?
The hyperlink control has target set to "_blank".
I'm using C# and Asp.net 4.5.
Upvotes: 3
Views: 1164
Reputation: 13652
You can specify a name in the target
attribute:
<asp:HyperLink
NavigateUrl="https://www.google.com"
Text="Go to Google"
Target="SameWindow"
runat="server"/>
<asp:HyperLink
NavigateUrl="http://stackoverflow.com"
Text="Go to StackOverflow"
Target="SameWindow"
runat="server"/>
HTML example:
<a href="http://www.google.com" target="SameWindow">Go to Google</a>
<br />
<a href="http://stackoverflow.com" target="SameWindow">Go to StackOverflow</a>
This is (I believe) identical to using the javascript window.open
function to pass in a name for a window. After the named window is opened once, subsequent calls to window.open
with that name passed in will load the new URL in the window that's already open.
Here's an example snippet. You might have to run the code snippet in the full JSFiddle page due to security restrictions (CORS).
var openTarget = function() {
console.log(this);
var target = this.getAttribute("attr-target");
window.open(target, "ExampleName");
}
var elems = document.querySelectorAll("button");
Array.prototype.forEach.call(elems, function(e) {
e.addEventListener("click", openTarget);
});
<button attr-target="https://www.google.com/">Open Google</button>
<button attr-target="http://stackoverflow.com/">Open StackOverflow</button>
Upvotes: 1
Reputation: 31
just set the target to some name like
<a href="http://google.com" target="anyname">a link</a>
Upvotes: 1
Reputation: 7
You can do it with the help of this simle code
<p onclick="window.open('www.sample.com','mywindow').focus()" >Visit Now</p>
Upvotes: 0