问题
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.org' 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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/28453959/how-can-i-set-the-browser-tabs-anchor-target-name-via-javascript