5
<script>
    $(document).ready(function () {
        $("#button").click(function () {
            window.location.href = "page2.aspx"; 

            $('html, body').animate({ 
                scrollToElement ("#div").offset().top 
            }, 2000); 
        }); 
    });
</script>

The idea, is that you click a button on page1, then you get redirected to page2 and then you scroll to a specific element using jQuery.

Mechlar
  • 4,946
  • 12
  • 58
  • 83
gabor_Csapo
  • 63
  • 1
  • 3
  • 1
    Please post any existing code [**You have tried so far**](http://whathaveyoutried.com) and point to the specific code which is causing you issues. – Nope May 26 '13 at 07:14
  • check this out: http://stackoverflow.com/questions/4884839/how-do-i-get-a-element-to-scroll-into-view-using-jquery – highwingers May 26 '13 at 07:18
  • well, i have tried a lot so far, and none on the was really effective. The most i got, is that it just redirected to page2. – gabor_Csapo May 26 '13 at 07:47
  • – gabor_Csapo May 26 '13 at 07:48
  • @user2421750: You can edit your question to add code or information. I have done that now. In general that is how you would go about it. Post the code then explain what is not working and what you are trying to achieve. Glad you got it sorted though. – Nope May 26 '13 at 08:20

1 Answers1

13

You can always set an anchor for that element, like so

<a name="scroll"></a>
<h2>I wanna scroll here</h2>

And link to it: http://mydomain.com/index.php#scroll

The only benefit in using jQuery for this task would be to animate the scrolling itself, in which case you do something like this:

<h2 id="scrollhere">I wanna scroll here</h2>

Link here: http://mydomain.com/index.php#scrollhere

And then in jQuery in the redirected page:

$(document).ready(function() {
    if (window.location.hash != null && window.location.hash != '') 
        $('body').animate({
            scrollTop: $(window.location.hash).offset().top
        }, 1500);
});
casraf
  • 21,085
  • 9
  • 56
  • 91
  • I'm not really in to jQuery, So if you could give me a little bit more help, and specify this "link here" part, i would be really thankful – gabor_Csapo May 26 '13 at 07:26
  • 1
    Just link like you normally would. Anything beyond the `#` is called a `hash`. If there is an `a` tag with the same name, it will automatically, immediately scroll to it. The jQuery solution is the same idea, only it animates to it (see [jQuery.animate](http://api.jquery.com/animate) and [jQuery.scrollTop](http://api.jquery.com/scrollTop/)). The 1500 is the time it will take to do the scroll in miliseconds, so 1.5 seconds (use whatever you like). The if at the start is to make sure the hash isn't blank, so as to not give an error when doing the scrolling as it will try to scroll to nothing. – casraf May 26 '13 at 07:30
  • Link here: (link) means link normally: `My link` – casraf May 26 '13 at 07:31