1

I have a JQuery function where I want to reload my web page and scroll it to a specific div afterward.

I did this

$(document).ready(function() {
  $('#mybutton').click(function(e) {
    location.reload();
    $('html, body').animate({
      scrollTop: $("#myID").offset().top
    }, 1000);
  })
})

When I run my script, my page reloads very well but the scroll script doesn't run afterward like I want.

Please help

Sagar Kodte
  • 3,723
  • 4
  • 23
  • 52

3 Answers3

2

I guess we can use a cookie to make it work as we wish.. Im adding a small sample code .. Please check it out.. Please comment if you need more help.. If this solves ur problem then up voting this as answer will be much appreciated.. Thanks

$(document).ready(function() {


  $('#mybutton').click(function(e) {

      //$.cookie("reload", 1);  // you can use this type of cookie if u have a jquery cookie plugin how ever i have added both type ..

      document.cookie=1; // I know this is the absurd way to use cookies. just thought of solving your problem. if you need precise result then either use a jquery cookie plugin or use setCookie function.
      reload=true;
    location.reload();


  })
  console.log(document.cookie);
  if(document.cookie==1) //if($.cookie("reload")==1)
  {
      document.cookie="0";
      console.log(document.cookie);
  $('html, body').animate({
      scrollTop: $("#myID").offset().top
    }, 1000);
  //$.cookie("reload", 0);
  }
})
  • So lets hope that it helped you.. Good Luck – Nair Ranjith Jun 17 '16 at 13:12
  • Theres no setCookie function in JS – Xatenev Jun 17 '16 at 13:23
  • I know bro.. we need to create one .. using functions.. If u need i can provide you one.. or u can see here [http://stackoverflow.com/questions/1458724/how-do-i-set-unset-cookie-with-jquery](http://stackoverflow.com/questions/1458724/how-do-i-set-unset-cookie-with-jquery). Sorry for Misconception. Thanks – Nair Ranjith Jun 17 '16 at 13:25
  • The most important thing was the algorithm, I just replace document.cookie = 1 by $.cookie("reload", 1); using JQuery Cookie Plugin and it works insanely great. Thx bro –  Jun 17 '16 at 17:20
  • 1
    I'm glad that I was helpful for u.. Good luck.. ~Nr – Nair Ranjith Jun 17 '16 at 17:33
1
$(document).ready(function() {
  $('#mybutton').click(function(e) {
    location.reload();
  });
  $('html, body').animate({
      scrollTop: $("#myID").offset().top
  }, 1000);
});

if you didn't want reload the page then on click it will work properly for that needs some modifiacation in code

$(document).ready(function() {
  $('#mybutton').click(function(e) {

    $('html, body').animate({
      scrollTop: $("#myID").offset().top
    }, 1000);

  });
});
Bhavya shah
  • 148
  • 8
  • Thanks for helping me, but the problem with your script is that, my window is scrolling whenever I reload the page, whereas I only want it scrolling when I click on my button and when location.reload() is called. –  Jun 17 '16 at 13:07
0

try my solution inside your div add this type of link

<a name="exactline">Linking Directly to a Specific Location</a>

and when you call window reload do it this way

window.location.replace("http://yuorsite/yourCurentPage.html#exactline");
C Florin
  • 99
  • 5