简体   繁体   中英

How to link to some region of a (big) web page

This should be a not so rare scenario, I wonder if there is some more-or-less standard solution or trick.

I want to place a link in my blog to a external web page, which is quite big in content (say, the full text of a book from gutenberg.org). I wish to point the visitor to a specific location inside that content (say, some paragraph), but the page has no adequate named anchors, so I can't simply append a fragment id to the url.

Isnt' there any way, perhaps with javascript? I would be happy with any trick that allowed me to tell the remote browser to open the page and either:

  • scroll to a vertical scrolling position (in percentage) - i know that this would only give an approximate placing, but it would be something
  • emulate the result of a client-side "find text inside page"
  • tell the browser to go to a specific position (counted in bytes or characters inside the html source) (not possible, I guess: javascript only sees the webpage as a DOM tree, not aa stream of byts/chars)

I'm open also to some serverside work (say, point the url to some php in my server that makes the redirection), but I don't see this would help.

Any ideas?

Update: I'm also interested in portions of plain text documents (instead of html). Quoting from here

Specific syntaxes for representing fragments in text documents by line and character range, or in graphics by coordinates, or in structured documents using ladders, are suitable for standardization but not defined here.

Have they been defined/implemented somewhere else?

Apart from named anchors <a name="foo"> , you can also use element ID's as anchors.

So if there is for example a

<div id="foo">

in the page, then you could use http://example.com/page.html#foo to jump to it. Apart from that, there are no other ways than including it in an <iframe/> .

Some of my own findings:

  • Basically, not possible, as BalusC points out.

  • Making a web page which includes the remote page in a IFRAME and scrolling it to the desired place: hardly practical, cumbersome, specially when the remote page is in another domain. And, specially, it's difficult (practically impossible, I believe) to compute the desired scrolling amount.

  • An extreme solution: make a proxy.php script which serves the remote page rewriting it, adding some id/anchors, say, at some percentage steps, counting characters. Shortcomings too numerous and evident to list.

  • The kind of feature I was seeking seems have been discussed, but only to exist in the " proposal " area. The most fitting and powerful solution seems to be this Fragment Search Greasemonkey script. But, of course, that must be installed in the browser; it is presently not useful to make public urls to be loaded by visitors.

Depending on context, I think you could achieve this kind of behavior with an extension to your user's browser.

When you click on the link, either the user is directed to the url without scrolling, or the extension is called which loads the page and performs a script on the page, add anchors or scrolling or whatever.

Of course, this only works for users who accept to download your extension and it's fairly complicated to do because you have to make one for every browser... That's why I said "depending on context" ;)

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