Home  >  Q&A  >  body text

Setting window.name in JavaScript to use <a target='name'> later from another page?

In a window, such as foo.example.com:

Then in another window, later, for example bar.example.com:

GOFOO

Will this work? Is there a way to return to a window that was not originally named with target=''?

P粉821808309P粉821808309398 days ago582

reply all(1)I'll reply

  • P粉434996845

    P粉4349968452023-09-17 22:32:37

    The name attribute of the window can indeed be set in JavaScript. However, it doesn't work exactly the way you imagine.

    In HTML, the target attribute of the element is used to specify where to open the linked document. The value of this attribute can be:

    _blank: Open the linked document in a new window or tab. _self: Open the linked document in the same frame as when clicked (this is the default). _parent: Open the linked document in the parent frame. _top: Open the linked document in the entire body of the window. framename: Open the linked document in the named frame. Therefore, the value of the target attribute is interpreted as the frame name, not window.name. They are not directly related.

    However, window.name plays a certain role in cross-window communication. When you navigate from one page to another, window.name remains the same (even when navigating across domains) until the window or tab is closed, or the window or tab navigates to a different domain where window.name is cleared page. But it won't work the way you're trying to use it in your example.

    It should be noted that the window.name attribute is mainly used for script programming, not as a way to reference windows between pages through the tag.

    What you are trying to achieve - referencing an existing window/tab from another window/tab - is often not directly possible due to limitations of the Same Origin Policy and restrictions on inter-window communication for security reasons.

    However, you can use other methods, such as using localStorage, sessionStorage, or postMessage to achieve some form of inter-window or inter-tab communication, depending on your needs.

    reply
    0
  • Cancelreply