最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Go back to previous page without refreshing using jQuery - Stack Overflow

programmeradmin5浏览0评论

I am running a script locally and in one of the functions, it goes to different page. Once it does that, I want it go to back to previous page and continue running the script locally.

Is there a way to go back to previous page without refreshing the page. I used parent.history.back(); this goes back to previous page but refreshes the page so my script stops running.

I am running a script locally and in one of the functions, it goes to different page. Once it does that, I want it go to back to previous page and continue running the script locally.

Is there a way to go back to previous page without refreshing the page. I used parent.history.back(); this goes back to previous page but refreshes the page so my script stops running.

Share Improve this question edited Mar 7 at 20:01 isherwood 61.2k16 gold badges121 silver badges170 bronze badges asked Sep 23, 2016 at 21:24 Learn AspNetLearn AspNet 1,6428 gold badges46 silver badges105 bronze badges 2
  • When your browser leaves a page and loads a new page like this, any scripts running are terminated. They will not pick up where they left off under any circumstances. The answers with location.href, location.replace, and history.back are incorrect and, had they been tested at all, would have been proven not to work. – Stephen P Commented Sep 23, 2016 at 22:21
  • what does "continue running the script locally" actually mean? Is this a full page or an angular route? there's a big difference – charlietfl Commented Sep 23, 2016 at 23:24
Add a ment  | 

4 Answers 4

Reset to default 4

Is there a way to go back to previous page without refreshing the page?

TL;DR - The short answer is "No"
    There is no way to go back without "refreshing" the page.

"refresh" is a somewhat vague term without an exact technical meaning ...

Going "back" by any means, whether it's the browser's back button or your .history.back() tells the browser to load the previous page it holds in its history.

Whether the page is loaded from the browser cache or re-requested from the server is up to the browser, not up to you.

In any case, it is again up to the browser whether it will re-parse the DOM and/or re-render the page. Which, in reality, it is going to do.

Any of these could be called "refresh".

At that time, however, the browser will start parsing and executing any scripts present. It will not continue wherever it was in the scripts at the time the page unloaded under any circumstances.

The page the browser goes back to is the HTML text as it was received from the server, but scripts could have significantly modified the DOM after the page was loaded. Those scripts need to run again, from the beginning, when the page is reloaded by going back.

It's conceivably possible to write a browser that saves the DOM state and js execution state when you leave a page, and restore that state when you return, but no such browser exists.


Depending on what your actual goals are for this, there are many things that could be done such as pushState() and replaceState(), single-page web applications, XMLHttpRequest, using <iframe>, etc. where you could replace the current page content (DOM) with other content without actually going "forward", and restore the saved DOM later when you "return" to the page,
but that's far too large a topic for a Stackoverflow question.

An easy way to avoid refreshing the previous page is to open the new page in a second tab in your browser. You just need to insert in your html code inside the specific tag the following code

target= "_blank"

This way you will keep the status of your previous page.

This can be done by using history api of JS. history.pushState()

this history api provide us a pushState method which can take a data, title and a url where it should redirect. this is used by react-router-dom behind the scene too

I'm not 100% following your question, but from my notes I can offer you this:

// To some url
window.location.href = 'some/new/url';
// To some url without it effecting browser back history:
window.location.replace('some/new/url');
发布评论

评论列表(0)

  1. 暂无评论