0

I'm currently using this script:

window.onbeforeunload=function(){window.scrollTo(0,0);}

Edited: I want to scroll the page to the top when triggering a page refresh. The problem is that the script is also working/executing when a user clicks on an a href element which is a download link from a subdomain that represent a downloadserver (that is hosted on external host) - let's say downloadserver.mydomain.com

<a href="xxx.mydomain.com/filetodownload.extension">

Is it possible to exclude the triggering when user click on such link with that specific subdomain?

Thank you.

Fueled By Coffee
  • 2,127
  • 6
  • 25
  • 40
Dorel
  • 71
  • 8

3 Answers3

0
 window.onbeforeunload=function(){
  if (window.location.href.indexOf('xxx.mydomain.com') >= 0)
     window.scrollTo(0,0);
 }
Joel Blum
  • 6,319
  • 8
  • 37
  • 53
0

If you just want your page to always be scrolled to the top after the page is refreshed, you could just run this code when the page's document is ready:

window.scrollTo(0,0);

So run it from a script tag before the body closes, or wait for the document to be ready, with or without jQuery.

Community
  • 1
  • 1
Alexander O'Mara
  • 52,993
  • 16
  • 139
  • 151
0

I don't know that it's a proper answer but works for you

1.Just have a global variable say isRefreshed.

var isRefreshed = true;

2.Have a anchor click listener

$(document).on('click', 'a', function(){
  if($(this).attr('href') !== "your current page url"){
     isRefreshed = false; //i.e clicked another url
  }
});

3.And finally your scroller

window.onbeforeunload=function(){
   if(isRefreshed) window.scrollTo(0,0); //calls only when refreshed
}

This is a simple hack. Hope this helps

Jyothi Babu Araja
  • 8,434
  • 3
  • 28
  • 37