简体   繁体   中英

Open a link in new tab and go to a specific div

I want to open a link in a new tab and go to a specific div in that link. The link is:

http://paper.li/f-1333760510

and I want to go to the div #paper-window on clicking the link. Here is what I have tried:

1.

<a href="http://paper.li/f-1333760510#paper-window" target="_blank" />

2

<a href="#" onclick="load-link(); return false" />

function load-link() {
    var w = window.open("http://paper.li/f-1333760510");
    w.location.href = "http://paper.li/f-1333760510#paper-window";
} 

The link looks ok... but you are misplacing the location.hash with the id of an element...

OLD: your anchor markup is absolutely ok:

<a href="http://paper.li/f-1333760510#paper-window" target="_blank">open me</a>
<a name="paper-window"></a>

cause the #something part of the url is a jump point to a certain anchor on that page and not pointing to an id

UPDATE (regarding your comments, thanks to all of you :) ) the HTML5 spec for Navigating to a fragment identifier indeed says both will work... where the order is to first look for an id and afterwards for an anchor with attribute name (although deprecated in HTML5 for backward compatibility)

So this would mean opening http://www.example.com#foo would result in a search for:

// check for an element with an id matching "foo"
<div id="foo"></div>

// if no element was found check for an anchor with that name (also backward compatibility)
<a name="foo"></a>

// compare location.hash with "top"
// would jump to the top of the page if it matches

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM