change url on scrolling using jquery/javascript -
change url on scrolling using jquery/javascript -
i want alter url when scrolling bar without using # . code:-
<a class = "section" href = "#first" > first paragraph < /a> < p > < /p> < class = "section" href = "#second" > sec paragraph < /a> < p > < /p> < class = "section" href = "#third" > 3rd paragraph < /a> < p > < /p> < class = "section" href = "#fourth" > 4th paragraph < /a> function isscrolledintoview(elem) { var docviewtop = $(window).scrolltop(); var docviewbottom = docviewtop + $(window).height(); var elemtop = $(elem).offset().top; var elembottom = elemtop + $(elem).height(); homecoming ((elembottom <= docviewbottom) && (elemtop >= docviewtop)); } $(window).scroll(function(e) { var anchors = $('.section'); (var = 0; < anchors.length; ++i) { if (isscrolledintoview(anchors[i])) { var href = $(anchors[i]).attr('href'); location.hash = href.slice(href.indexof('#') + 1); break; } } }); $(function() { var hash = location.hash; if (hash) { // scroll element given hash. var anchors = $('.section'); (var = 0; < anchors.length; ++i) { if ($(anchors[i]).attr('href') === hash) { $(window).scrolltop($(anchors[i]).offset().top); break; } } } }); when run code url show this:
file:///c:/users/jony/desktop/demo.html#third but want show :
file:///c:/users/jony/desktop/demo.html/third
if want create new history entry (i.e. user can click button go back), use
window.history.pushstate(state, title, newurl); otherwise, use
window.history.replacestate(state, title, newurl); if don't know state , title, can pass null or empty string.
https://developer.mozilla.org/en-us/docs/web/guide/api/dom/manipulating_the_browser_history
javascript jquery html html5
Comments
Post a Comment