Danny Ellis Jr.
Danny Ellis Jr.

Reputation: 1706

Scroll To Anchor Tag When using knockoutjs

I'm using knockout to populate a webpage in my project, and I want to be able to go to an anchor tag in the page that is bound to by a knockoutjs property. When when I hit this page from outside with an anchor tag, "Book/Main/1#mytag", it does not scroll to this tag I'm guessing because that tag does not exist on the page yet.

How can I hit this page from outside, let the page populate, then scroll to this anchor tag?

Thanks!

Upvotes: 0

Views: 245

Answers (1)

Roy J
Roy J

Reputation: 43899

After you applyBindings, you just need to clear and re-set location.hash.

// Save it
var scrollTag = location.hash;
// ... Define your vm and whatnot ...
ko.applyBindings(vm);
//clear hash
location.hash = '';
//re-set it
location.hash = scrollTag;

Fiddle

Upvotes: 1

Related Questions