简体   繁体   中英

Jump to anchor after JSON load

I have a page that pulls data in via JSON. The issue is linking to content on the page that has a specific anchor. The browser looks for the anchor before the JSON is loaded and when it can't find it, it fails. Data is then loaded via JSON but the page never jumps to the anchor because it has already made the attempt to locate the anchor on the page load.

Let's say I want to link to a div way down the page:

<div id="linkToMe">Content</div>

Then I create the url to hyperlink to this content at an external source (ie a word document, email, or whatever):

http://www.example.com/specificpage.html#linkToMe

When someone navigates to this link, the issue above occurs since the JSON data takes a while to load.

I'm assuming to get this to work I'll need javascript to grab the hash in the url then jump to that hash after the JSON is fully loaded? What would that code look like, and how would I implement it to run after the JSON has been loaded in its entirety?

To handle your URL

var jumpID = url.split("#")[1];

Since you are using jQuery, you can use $.get() method to get data and write code in the callback function which is executed after your JSON is loaded.

$.get( "giveMeJSON.com", function( data ) {
   document.getElementById(jumpID).scrollIntoView();
});

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