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

javascript - Is possible to use target and window.name? - Stack Overflow

programmeradmin0浏览0评论

Scenario

Open new tab (). Enter into console:

window.name = "example-slave"

Open new tab (). Add HTML:

<a href="" target="example-slave">Click me!</a>

Expected effect

Added link should open example in first tab (example-slave).

Current effect

Added link is opening new window, and then every additional click opens example in this window.

Question

Is it normal? Is there any possibility to get expected effect?

Scenario

Open new tab (http://example./slave). Enter into console:

window.name = "example-slave"

Open new tab (http://example./master). Add HTML:

<a href="http://example." target="example-slave">Click me!</a>

Expected effect

Added link should open example. in first tab (example-slave).

Current effect

Added link is opening new window, and then every additional click opens example. in this window.

Question

Is it normal? Is there any possibility to get expected effect?

Share Improve this question edited Jul 3, 2015 at 8:30 marverix asked Jul 3, 2015 at 7:43 marverixmarverix 7,7056 gold badges40 silver badges52 bronze badges 10
  • 1 Nopes, you cannot set the name, but you can only get the name. It is just a getter, and not a setter. – Praveen Kumar Purushothaman Commented Jul 3, 2015 at 7:45
  • 4 Welp, w3schools and MDN says something else ;) w3schools./jsref/prop_win_name.asp developer.mozilla/en-US/docs/Web/API/Window/name – marverix Commented Jul 3, 2015 at 7:53
  • 1 I think your presented scenario shouldn't be possible, because it would require a cross-window, cross-origin global context in which window names would be shared and exposed to naming conflicts. – pawel Commented Jul 3, 2015 at 8:01
  • 1 (can't edit, but to clarify my previous ment) For example a website names its window for whatever reason (Facebook does, for example) and if one knew that name one could set the target to that name thus making users leave the other website unwillingly. And what if there are more than windows (several instances of the same website that sets it window.name to a static string)? Which one should the browser target? – pawel Commented Jul 3, 2015 at 8:09
  • 1 @marverix if you open both tabs manually and navigate to the urls there's no way the browser would let one tab know anything about the other. Only way to interact with other tabs/windows is creating them with JS. – pawel Commented Jul 3, 2015 at 8:35
 |  Show 5 more ments

2 Answers 2

Reset to default 6

See the specification:

If the given browsing context name is not _blank and there exists a browsing context whose name is the same as the given browsing context name, and the current browsing context is familiar with that browsing context, and the user agent determines that the two browsing contexts are related enough that it is ok if they reach each other, then that browsing context must be the chosen one.

The problem with your scenario is that the two browsing contexts were opened by the user using browser controls. They aren't familiar with each other and are not related at all, so they can't modify each other.

For them to be related you would need to open one from the other using target or window.open().

I think your presented scenario shouldn't be possible, because it would require a cross-window, cross-origin global context in which window names would be shared and exposed to naming conflicts. For example a website names its window for whatever reason (Facebook does, for example) and if one knew that name one could set the target to that name thus making users leave the other website unwillingly.

Your best bet is to rely on creating a new window using window.open, keepeing a reference and changing the location.href of the window:

HTML:

<a href="/?something" target="mytarget">Open a link in new window</a> 
<a href="/?anotherthing" target="mytarget">Open a link in opened window</a>

JS:

var targetlinks = document.querySelectorAll('[target="mytarget"]');

var opentarget = function(e){
    if( "mytarget" in window ){
         mytarget.location.href = e.target.href;   
    }
    else {
         window.mytarget = window.open( e.target.href, 'mytarget' );   
    }
    e.preventDefault();
};

[].slice.call( targetlinks ).forEach( function(link){
    link.addEventListener('click', opentarget );
});

(can't make this work in jsfiddle, probaly because of frames, but works fine on my local web server).

发布评论

评论列表(0)

  1. 暂无评论