I want to use a jquery on load function and I want it to scroll down to the anchor if the url is, for example:
url = http://test.aspx?section=section2
How can I achieve this through jquery? (I don't want to use anchor links because I'm using asp)
I want to use a jquery on load function and I want it to scroll down to the anchor if the url is, for example:
url = http://test.aspx?section=section2
How can I achieve this through jquery? (I don't want to use anchor links because I'm using asp)
If you want to do it with jQuery, you need the following code from this question. It is to get querystring parameters from the url. Then if you have the correct parameter you can scroll to it's position.
<div style="height: 10000px">Large spacer</div>
<div id="section2">Scrolled to here!</div>
<script>
$(document).ready(function () {
var offset = $('#' + getParameterByName('section')).offset().top;
$('html, body').animate({
scrollTop: offset
}, 1000);
});
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
</script>