最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How can I set the browser tab's anchor target name via javascript? - Stack Overflow

programmeradmin4浏览0评论

When my website is called from an external hyperlink it shall be re-opened within the same browser tab, if it is already open. This can be achieved by specifying an appropriate target attribute within the hyperlink's HTML: <a href='' target='my_target' /> However, this does not work, if the first tab is opened without a target being specified, e. g. by directly typing in the website's url. After this the hyperlink using my_target will pull up a second browser tab.

So, I want to set a default target name of the browser tab, if none exists, to ensure that this tab is used, if this default target is used in hyperlinks.

How can this be done in JavaScript?

When my website is called from an external hyperlink it shall be re-opened within the same browser tab, if it is already open. This can be achieved by specifying an appropriate target attribute within the hyperlink's HTML: <a href='http://my-website' target='my_target' /> However, this does not work, if the first tab is opened without a target being specified, e. g. by directly typing in the website's url. After this the hyperlink using my_target will pull up a second browser tab.

So, I want to set a default target name of the browser tab, if none exists, to ensure that this tab is used, if this default target is used in hyperlinks.

How can this be done in JavaScript?

Share Improve this question asked Feb 11, 2015 at 12:06 Sir HackalotSir Hackalot 5416 silver badges16 bronze badges 1
  • Not likely. If you could you could essentially tell what tabs a user had open. – mattsven Commented Feb 11, 2015 at 12:19
Add a ment  | 

2 Answers 2

Reset to default 5

Think all you have to do is

window.name = 'my_target'

If this is the first tab it will give mark this as 'my_target'. When any link with

target='my_target'

is now clicked it will opened in this tab/window.

Put an underscore in front of the name: _my_target. That has always worked for me. However, I do see that names other than the standard ones (_blank, _parent, _self, _top) are known as a framename, and frames are now deprecated. Still, any custom name with a leading underscore does seem to work. The advantage is that links to the same named target will give focus to the associated browser tab. The bad news is that it will probably stop working some day as browsers update.

发布评论

评论列表(0)

  1. 暂无评论