2

How do I create a link to a part of long webpage on another website that I don't control, that doesn't have an anchor tag?

I am trying to create a Course Outline Finder chrome extension for my university that you can use to:

  • Type course code in input box. (Use JS to filter out all other course codes)
  • Click the course code that remains after entering course code.
  • Course code link then leads you to a specific part of the university webpage that shows a list of course outlines for that specific course.

Ideally the webpage would have given an anchor tag like the following:

<h2 id="anchor">COMP150</h2>

Which I would then be able to link by doing the following:

<a href="https://www.ufv.ca/calendar/CourseOutlines/PDFs/COMP/#anchor>

But the website unfortunately doesn't have any id's for the h2 tags. It instead has this:

    <h2>COMP 150</h2>
      <ul>
        <li><a href="COMP150-20000927.pdf">COMP150-20000927.pdf</a>Effective Fall 2012</li>
        <li><a href="COMP150-20011207.pdf">COMP150-20011207.pdf</a>Effective Fall 2019</li>
      </ul>

Is there anything I can do?

  • 1
    I found a solution using text (instead of anchors) by JOUTI: https://stackoverflow.com/questions/2835140/how-do-i-link-to-part-of-a-page-hash#:~:text=JOUTI – Lyu Hiroyama Oct 04 '22 at 02:40

2 Answers2

3

You can insert the ID yourself in your extension:

document.querySelectorAll("h2")
  .forEach(header => header.id = header.id ? header.id : header.textContent.trim())

Alternatively ask them to add an ID to their headers - they might agree

mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

I found a solution using text (instead of anchors) by JOUTI here:

https://stackoverflow.com/questions/2835140/how-do-i-link-to-part-of-a-page-hash#:~:text=JOUTI

Test:

https://stackoverflow.com/questions/2835140/how-do-i-link-to-part-of-a-page-hash#:~:text=Click%20on%20this%20link

  • That actually does not work for me from the link here. It works from elsewhere The [link Jouti posted does work](https://stackoverflow.com/questions/2835140/how-do-i-link-to-part-of-a-page-hash#:~:text=Click%20on%20this%20link) – mplungjan May 23 '23 at 06:09
  • 1
    That's odd. Seems to work in comments but not on an answer post. I edited my answer to copy-paste your link. The link via your comment works, but won't via answer post. I wonder why. – Lyu Hiroyama May 24 '23 at 08:55