93

I am trying to refresh the page with this statement in external javascript file after some processes.

window.location.href = window.location.href

It perfectly reload the page but I want to scroll to the specific location after reload. So, I put this on the page.

<a name="uploadImgSection"></a>

And change the javascript to

window.location.href = window.location.href + "#mypara";

This code doesn't reload/refresh the page either

window.location.hash = "#uploadImgSection";
window.location.href = window.location.href;

When I do that, it doesn't reload the page at all. Is there any way I can reload the page with scroll bar position?

3
  • Use a PHP file to redirect to the new page? May 16, 2012 at 5:20
  • No just html page. The purpose is refresh the page after running some javascript functions and scroll it back to that paragraph's position.
    – Jonas T
    May 16, 2012 at 5:21
  • 1
    how are you calling window.location.href? if you are doing that on page load and you are hitting f5 and you are using firefox (or other browser that scrolls where you were before the f5 on refresh) this might be the problem
    – ajax333221
    May 16, 2012 at 5:35

8 Answers 8

174
window.location.href += "#mypara";
location.reload();

First add the hash, then reload the page. The hash will remain there, and the page gets reloaded.

Tested, works.


ps: If a hash already exist in the URL, you may want to directly change it via location.hash instead of .href.

9
  • 1
    One other thing. It did refresh the page but it includes post data after the reload. How can I clean those? Those post data are preventing me to delete the data row.
    – Jonas T
    May 16, 2012 at 5:50
  • Please read this. stackoverflow.com/questions/2405117/… It does include those POST data which is preventing me to do other postback events.
    – Jonas T
    May 16, 2012 at 5:53
  • Well, I don't know much about POST. You can ask a new question about it though, if you want to. May 16, 2012 at 6:14
  • 1
    Well, I had to use query string at last. "window.location.href = url + "&action=reload";". Check action query string in javascripot on page load event, and do this "window.location.hash = "#uploadImgSection";". It's not the way it should work but I had to this way to make this work for now.
    – Jonas T
    May 16, 2012 at 6:23
  • Yup Genius in our midst :) Best solution Sep 15, 2016 at 22:09
42

I wanted to update this question because I found that using window.location.href += "#mypara" will keep appending the parameter to the url even if it exists. I have found the better way is to use

window.location.hash = "#mypara"
location.reload();
30

This is really an old post, I would still try and answer with right combination of answers.

  1. location.reload() and location.reload(true) works like F5 on browser. This will post all the form data back to the server if previously load had done it.
  2. location.href will not refresh the page until there is a URL change recognized by the browser. change in hash (#paramname) doesn't qualify for URL change and hence just doing location.href+="#paramname" will not work.

So, location.href+="?anything#paramname" should reload the page as a new request as ?anything is change in the URL.

7
  • Nice answer. Here is a cooked version: window.location='<?php echo $_POST['back'] ?>&rand='+((new Date()).getTime()%1000000000)+'#myhash';
    – Johan
    Nov 11, 2013 at 15:37
  • 4
    Another one: location.href+='&rand='+Math.rand()+'#myhash';
    – Johan
    Nov 11, 2013 at 15:40
  • IMO this answers the question the best compared to the other posts. It requires no change to the underlying code that may have been written as part of a library for example. Dec 15, 2015 at 20:37
  • 1
    Johan, Math.rand() should be changed to Math.random()
    – tomJO
    Apr 12, 2018 at 13:35
  • 1
    The closest answer (maybe the other answers were correct in 2012, but they're all wrong in 2018), but it has the problem of adding entries to the user's address bar history, which isn't good if you were just trying to refresh the same history state... Jun 27, 2018 at 6:22
1
var loc = location.hash;
location.hash = " ";
location.hash = loc;
1
  • Code-only answers are not very useful. What is this doing? Why does it work? Aug 16, 2019 at 16:00
-1

Try this

           var urlString=window.location.href;
           var url=urlString.split("#")[0];
           window.location.href = url + "#mypara";
2
  • 4
    This doesn't reload the page at all either.
    – Jonas T
    May 16, 2012 at 5:35
  • "Try this" code only answers aren't very useful. What's it doing? Why does it work? Aug 16, 2019 at 16:00
-1

This worked for me:

var tabValue = document.URL;
window.location = tabValue.substring(0, tabValue.lastIndexOf("#"));
window.location.hash = "#tabs-3"; //Whatever is your hash value
location.reload();
-1

This work for me

window.location.href = baseUrl + "#/m/team";
 location.reload();
-1

This worked for me

$('body').on('click', '.className', function () {
    var data = $(this).attr('href');
    alert(data);
    window.location.reload();
});
1
  • This seems to basically be a longer way of doing what other answers already do, plus I don't think it includes the #myPara part so won't work like the original poster wants it to, although please edit the answer to clarify that if I'm wrong.
    – blm
    Nov 18, 2015 at 19:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.