How to link to specific line of text in page, using only URL?

前端 未结 4 1783
庸人自扰
庸人自扰 2020-12-10 06:42

I am curious if there is a way to use only a URL to send someone to a specific line at a given page. Citebite does this, however it links to a cached page instead of the act

相关标签:
4条回答
  • 2020-12-10 07:19

    As far as my knowledge goes, you cannot do it natively without some sort of caching, plugins or addons or having full control over html where you can embed anchor tags.

    0 讨论(0)
  • 2020-12-10 07:19

    There is an experimental feature in Chrome 74 called Scroll To Text Fragment which allows you to specify phrases to be highlighted in a URL. We may have to wait a while before this becomes finalised and supported in browsers by default.

    Meanwhile, if the article to which you want to link also has a PDF version, you could use a URL like https://example.org/file.pdf#page=2&search=findme to highlight findme on page 2.

    0 讨论(0)
  • 2020-12-10 07:41

    You must give an ID to your element. Example: <div id="footer">Footer</div>. If you go to mypage.html#footer, the browser will get you there.

    If it's not your webpage, look in the code, you may find an ID element next to the text you want.

    0 讨论(0)
  • 2020-12-10 07:42

    Depends on the situation:

    1-Your own content

    a. Simply add a href id, and then send a link to that (http://exmaple.com/document.php#myLine)

    b. For a more advanced solution, you can scroll to a specific area and highlight it using javascript, basically you call something like this: http://example.com/document.php?scrollX=100&scrollY=230

    and in your PHP code you have a function like this:

    <?php
    if(isset($_GET['scrollX']) && isset($_GET['scrollY')){
        ?>
           <script type="text/javascript">
               window.scrollTo(<?= $_GET['scrollX'];?>, <?=$_GET['scrollY'];?>);
           </script>
        <?
    }
    ?>
    

    You can also use JQuery and do more fancy things like highlighting text or changing CSS values depending on the arguments

    2-Someone else's content

    You cannot directly link to a specific line or area without a browser extension or an external service like CiteBite, however with some hackery you can build your own little service that displays the website and run your own JS on it (think iFrames or something like that)

    0 讨论(0)
提交回复
热议问题