admin管理员组文章数量:1201370
I have same ancors/hyperlink in my html file. These point to a new website outside my site. So I want that when the user clicks the link it should open a new tab or window. My website page should not be closed.
How can it be done?
I have same ancors/hyperlink in my html file. These point to a new website outside my site. So I want that when the user clicks the link it should open a new tab or window. My website page should not be closed.
How can it be done?
Share Improve this question edited Sep 24, 2009 at 6:41 eyelidlessness 63.5k12 gold badges92 silver badges95 bronze badges asked Sep 24, 2009 at 6:37 user177785user177785 2,2696 gold badges22 silver badges17 bronze badges 2- 3 Don’t do it. You should not impose your opinion about where the new URL should be opened upon your visitors. If I want a URL to open in a new tab I’ll use Ctrl-click, or 3rd mouse button, or whatever. Point is: I decide, not you. – Bombe Commented Sep 24, 2009 at 7:46
- But most of the websites does that only. Commercial benefits may be... – ivorykoder Commented Oct 18, 2010 at 4:23
6 Answers
Reset to default 10open in tabs: there is nothing programatically that you can do to accomplish that, the only thing I'm thinking of is set the browser to open new links in tabs instead of new window...
to open in a new window all you need is to place a target in your anchors
<a href="mydomain.com" target="_blank">click here</a>
and, by the way there are more options to the target
target="_blank"
opens in a new Blank window
target="_parent"
opens in a Parent window (used when dealing with iframes and you want to open the link in other frame)
target="_self"
opens in it's own/self window (used when dealing with iframes and you want to open the link in the current frame)
target="_top"
opens in the top of all frames (it will open on top of all frames in the page, like a no-frame page will be display)
<a href="newpage.htm" target="_blank">Click me to open in a new tab/window</a>
Load the linked document into a new blank window. This window is not named.
If there is no existing window or frame with the same name as specified in the target, a new window is opened with a name equal to the value of the target.
Its the setting in a browser that determines whether to open the page in a new tab or in a new window.
Just add target="_blank" to the <a> tag
<a href="http://example.com" target="_blank"> Click here </a>
The easiest way is to use the target
attribute in your anchors and set it to _blank
:
<a href="http://www.worsethanfailure.com/" target="_blank">Worse Than Failure</a>
This is a dupe of opening links in the same window or in a new (tab) and https://stackoverflow.com/questions/118567/is-there-ever-a-good-reason-to-force-opening-a-new-browser-window. Basically - no. And there shouldn't be a way - you shouldn't impose your surfing preferences on unsuspecting others.
You can add target="_blank"
for every <a>
element, whose href
attribute begins with http
, in a webpage by:
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('a[href^="http"]').each(function() {
$(this).attr('target', '_blank');
});
});
</script>
</head>
本文标签: javascriptHow can I open a new tab or window when a link is clickedStack Overflow
版权声明:本文标题:javascript - How can I open a new tab or window when a link is clicked? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738632914a2103857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论