135

How do I create a link to a part of long webpage on another website that I don't control?

I thought you could use a variant of the #partofpage at the end of my link. Any suggestions?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
mjmuk
  • 1,351
  • 2
  • 9
  • 3
  • 1
    Possible duplicate of [How do I link to part of a page? (hash?)](http://stackoverflow.com/questions/2835140/how-do-i-link-to-part-of-a-page-hash) – Quentin Nov 02 '16 at 16:37
  • To link this part of answer use following link, https://stackoverflow.com/questions/15481911/linking-to-a-specific-part-of-a-web-page – Ruturaj Bisure Dec 26 '17 at 13:25
  • To link this part of page use following link, https://stackoverflow.com/questions/15481911/linking-to-a-specific-part-of-a-web-page – Ruturaj Bisure Dec 26 '17 at 13:26
  • If this is needed in Atlassian Confluence, it's not very straight forward but, they've documented it here: https://confluence.atlassian.com/doc/anchors-139442.html – eby Jun 02 '21 at 15:49

9 Answers9

116

Just append a # followed by the ID of the <a> tag (or other HTML tag, like a <section>) that you're trying to get to. For example, if you are trying to link to the header in this HTML:

<p>This is some content.</p>
<h2><a id="target">Some Header</a></h2>
<p>This is some more content.</p>

You could use the link <a href="http://url.to.site/index.html#target">Link</a>.

Adirio
  • 5,040
  • 1
  • 14
  • 26
APerson
  • 8,140
  • 8
  • 35
  • 49
39

Create a "jump link" using the following format:

http://www.example.com/somepage#anchor

Where anchor is the id of the element you wish to link to on that page. Use browser development tools / view source to find the id of the element you wish to link to.

If the element doesn't have an id and you don't control that site then you can't do it.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
cowls
  • 24,013
  • 8
  • 48
  • 78
  • What if the specific part of the webpage does not have an id associated with it? – asn Dec 27 '18 at 12:55
  • Then you'll need to add the id to the element – cowls Dec 29 '18 at 18:58
  • 1
    Can I add an id to a tag on a web page on the internet? – asn Dec 30 '18 at 05:27
  • 2
    No, if you want to do this to another website you could try creating a custom JS bookmarklet to scroll to the part you want to scroll to. You can't make the anchored links work if the ids aren't setup. – cowls Dec 30 '18 at 15:25
  • Can you link to something that you specified above for doing that? I haven't done anything of that sort before. – asn Dec 30 '18 at 16:29
  • 1
    jay, its not possible to have a bookmarklet that navigates to a page and scrolls to certain position. Once your bookmarklet changes location, the page is reloaded so your js is lost. But, for your reference, a bookmarklet that scrolls on your current page to 500 pixels from the top would look like this: javascript:scroll({top:500}) – Frazer Kirkman Feb 15 '19 at 01:12
  • I think it is called an [anchor link](https://www.rapidtables.com/web/html/link/html-anchor-link.html) – Chagai Friedlander May 12 '20 at 11:04
11

That is only possible if that site has declared anchors in the page. It is done by giving a tag a name or id attribute, so look for any of those close to where you want to link to.

And then the syntax would be

<a href="page.html#anchor">text</a>
fredrik
  • 6,483
  • 3
  • 35
  • 45
9

In case the target page is on the same domain (i.e. shares the same origin with your page) and you don't mind creation of new tabs (1), you can (ab)use some JavaScript:

<a href="javascript:void(window.open('./target.html').onload=function(){this.document.querySelector('p:nth-child(10)').scrollIntoView()})">see tenth paragraph on another page</a>

Trivia:

var w = window.open('some URL of the same origin');
w.onload = function(){
  // do whatever you want with `this.document`, like
  this.document.querySelecotor('footer').scrollIntoView()
}

Working example of such 'exploit' you can try right now could be:

javascript:(function(url,sel,w,el){w=window.open(url);w.addEventListener('load',function(){w.setTimeout(function(){el=w.document.querySelector(sel);el.scrollIntoView();el.style.backgroundColor='red'},1000)})})('https://stackoverflow.com/questions/45014240/link-to-a-specific-spot-on-a-page-i-cant-edit','footer')

If you enter this into location bar (mind that Chrome removes javascript: prefix when pasted from clipboard) or make it a href value of any link on this page (using Developer Tools) and click it, you will get another (duplicate) SO question page scrolled to the footer and footer painted red. (Delay added as a workaround for ajax-loaded content pushing footer down after load.)

Notes

  • Tested in current Chrome and Firefox, generally should work since it is based on defined standard behaviour.
  • Cannot be illustrated in interactive snippet here at SO, because they are isolated from the page origin-wise.
  • MDN: Window.open()
  • (1) window.open(url,'_self') seems to be breaking the load event; basically makes the window.open behave like a normal a href="" click navigation; haven't researched more yet.
myf
  • 9,874
  • 2
  • 37
  • 49
  • huh it didn't scroll to the bottom and the bottom isn't red – Coder Aug 11 '19 at 02:53
  • 1
    …and neither it opened new window, I suppose. Most probably because fact, that execution of `javascript:` links typed into URLbar does not work in most browsers anymore with default settings, as "self-XSS" prevention. But you can try it in the Web Developers console, where it should work well. – myf Aug 13 '19 at 19:33
  • could someone please edit this and make it easier to use? I am not a javascript expert and couldn't figure out what to do – Chagai Friedlander Feb 26 '20 at 15:03
8

The upcoming Chrome "Scroll to text" feature is exactly what you are looking for....

https://github.com/bokand/ScrollToTextFragment

You basically add #targetText= at the end of the URL and the browser will scroll to the target text and highlight it after the page is loaded.

It is in the version of Chrome that is running on my desk, but currently it must be manually enabled. Presumably it will soon be enabled by default in the production Chrome builds and other browsers will follow, so OK to start adding to your links now and it will start working then.

Edit: It's been implemented in Chrome. See https://chromestatus.com/feature/4733392803332096

Community
  • 1
  • 1
bigjosh
  • 1,273
  • 13
  • 19
5

You can NOW...

As of Chrome release 81 (Feb 2020), there is a new feature called Text Fragments. It allows you to provide a link that opens at the precise text specified (with that text highlighted).

At the moment, it works in Edge, Chrome and Opera but not in Firefox, Safari or Brave. (See note 6 at bottom for more)

For security reasons, the feature requires links to be opened in a noopener context. Therefore, make sure to include rel="noopener" in your anchor markup or add noopener to your Window.open() list of window functionality features.

You create the link to your desired text by appending this string to the end of the URL:

/#:~:text=

and providing the percent-encoded search string thus:

 /#:~:text=String%20to%20focus%20on

Here is a working example:

https://newz.icu/#:~:text=Google%20surveillance%20increases

Notes:

  1. Test the above link in Chrome or Opera only

  2. In the above example, note that the text string is in a div that is normally hidden on page load - so in this example it is being displayed despite what would normally happen. Useful.

  3. Recent versions of Chrome also include a new option when you Right-Click on selected text: Copy link to highlight. This will auto-create the direct-to-text link for you (i.e. it automatically appends the /#:~:text= to the text you highlighted) and place it in the clipboard - just paste it where desired.

  4. Suppose you want to highlight an entire block of text? The Text Fragments feature allows specifying a starting%20phrase and an ending%20phrase (separated by a comma), and it will highlight all text in between:
    https://newz.icu/#:~:text=Dr.%20Mullis,before%20now
    Note the comma between Mullis and before

  5. web.dev article about Text Fragments

  6. CanIUse status of Text Fragments

PS - Please forgive choice of example website. It simply had the desired 
elements required for the demonstration. Hoping we can focus on function 
rather than content.
cssyphus
  • 37,875
  • 18
  • 96
  • 111
3

First off target refers to the BlockID found in either HTML code or chromes developer tools that you are trying to link to. Each code is different and you will need to do some digging to find the ID you are trying to reference. It should look something like div class="page-container drawer-page-content" id"PageContainer"Note that this is the format for the whole referenced section, not an individual text or image. To do that you would need to find the same piece of code but relating to your target block. For example dv id="your-block-id" Anyways I was just reading over this thread and an idea came to my mind, if you are a Shopify user and want to do this it is pretty much the same thing as stated. But instead of

> http://url.to.site.example/index.html#target

You would put

> http://example.com/target

For example, I am setting up a disclaimer page with links leading to a newsletter signup and shopping blocks on my home page so I insert https://mystore-classifier.com/#shopify-section-1528945200235 for my hyperlink. Please note that the -classifier is for my internal use and doesn't apply to you. This is just so I can keep track of my stores. If you want to link to something other than your homepage you would put

> http://mystore-classifier.example/pagename/#BlockID

I hope someone found this useful, if there is something wrong with my explanation please let me know as I am not an HTML programmer my language is C#!

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
1

Basically, html tag can have id="abc" as shown below:

<div id="abc">test</div>
<p id="abc">test</p>
<span id="abc">test</span>
<a id="abc">test</a>

And, "<a>" tag can also have name="abc" as shown below:

<a name="abc">test</a>

Then, you can use the id and name values "abc" with "#" in urls as shown below to go to the specific part of a page:

https://www.example.com/#abc
https://www.example.com/index.html#abc

Then, you can put the urls above in "<a>" tag to create the links to id="abc" and name="abc" as shown below:

<a href="https://www.example.com/#abc">test</a>
<a href="https://www.example.com/index.html#abc">test</a>

And, if you want to go to the specific part of the same page, you can only put the id and name values "abc" with "#" in "<a>" tag to create the links to id="abc" and name="abc" as shown below:

<!-- Go to the specific part of the same page -->

<a href="#abc">test</a>

<div id="abc">test</div>
<!-- Go to the specific part of the same page -->

<a href="#abc">test</a>

<a name="abc">test</a>
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
1

It's now possible to create an "anchor" link that goes to a specific part of any webpage in most browsers in a few different ways. All of them will create a link with an #anchor at the end, where "anchor" is the thing that you want to navigate to. The browser will interpret the part of the URL after the # to scroll to a specific part of the page.

Here are 3 ways to create a url like this:

  • Using an existing anchor. Perhaps there will be one in the URL as you scroll down the page. If not, look around the page for a header that has a little link icon to the left of it and click it to update the browsers navigation url.
  • Using any html element's id property or the name or id on an ("anchor") element. The other answers explain this quite well. You will have to open the developer console and inspect the part of the page to find an id (and you may not find one). It's a little different on each browser, but here's how to inspect an element in Chrome.
  • Using a text snippet to highlight part of the page.
DataSensei
  • 11
  • 4