Javascript 重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16248003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Reload Page
提问by Alasdair
In order to reload the page without fully reloading everything I'm using:
为了重新加载页面而不完全重新加载我正在使用的所有内容:
window.top.location=window.top.location;
However, this doesn't work for some reason when there is anchor text. It appears to do nothing in this case, but more likely it refreshes the anchor.
但是,当有锚文本时,由于某种原因,这不起作用。在这种情况下它似乎什么都不做,但更有可能刷新锚点。
Any fix for this that will reload the page (as above) without reloading the cached images and scripts?
是否有任何修复方法可以重新加载页面(如上所述)而不重新加载缓存的图像和脚本?
回答by icktoofay
Try using location.reload(false)
.
尝试使用location.reload(false)
.
As MDN says, the second parameter is a boolean indicating whether to bypass the cache or not. false
keeps using the cache, as you wanted.
正如 MDN 所说,第二个参数是一个布尔值,指示是否绕过缓存。false
继续使用缓存,如你所愿。
回答by Mike Samuel
icktoofay explains the workaround so I'm just addressing this question:
icktoofay 解释了解决方法,所以我只是在解决这个问题:
However, this doesn't work for some reason when there is anchor text. It appears to do nothing in this case, but more likely it refreshes the anchor.
但是,当有锚文本时,由于某种原因,这不起作用。在这种情况下它似乎什么都不做,但更有可能刷新锚点。
HTML 5 describes the Locationinterfacewhich covers assignment to document.location
.
HTML 5 描述了Location界面,其中涵盖了对document.location
.
When the assign(url) method is invoked, the UA must resolve the argument, relative to the entry script's base URL, and if that is successful, must navigate the browsing context to the specified url.
当调用assign(url) 方法时,UA 必须解析相对于入口脚本的基本URL 的参数,如果成功,必须将浏览上下文导航到指定的url。
So the navigate operation only sees an absolute URL, so there is no difference assigning just a fragment vs assigning an absolute URL that is the same as the page URL with a fragment.
所以导航操作只能看到一个绝对 URL,所以只分配一个片段与分配一个与带有片段的页面 URL 相同的绝对 URL 没有区别。
8 Fragment identifiers: Apply the URL parser algorithm to the absolute URL of the new resource and the address of the active document of the browsing context being navigated. If all the components of the resulting parsed URLs, ignoring any fragment components, are identical, and the new resource is to be fetched using HTTP GET or equivalent, and the parsed URL of the new resource has a fragment component that is not null (even if it is empty), then navigate to that fragment identifierand abort these steps.
8 片段标识符:将 URL 解析器算法应用于新资源的绝对 URL 和被导航的浏览上下文的活动文档的地址。如果生成的解析 URL 的所有组件(忽略任何片段组件)都相同,并且将使用 HTTP GET 或等效方法获取新资源,并且新资源的解析 URL 具有不为空的片段组件(即使如果为空),则导航到该片段标识符并中止这些步骤。
Finally, navigating to a fragment identifiersays
最后,导航到片段标识符说
When the user agent is required to scroll to the fragment identifier, it must either change the scrolling position of the document using the scroll an element into view algorithm defined in the CSSOM View specification, with the align to top flag set, or perform some other action, such that the indicated part of the document is brought to the user's attention.
当用户代理需要滚动到片段标识符时,它必须使用 CSSOM 视图规范中定义的将元素滚动到视图算法更改文档的滚动位置,并设置对齐到顶部标志,或者执行其他一些操作动作,从而引起用户注意文档的指示部分。
回答by Joseph Myers
The best way to be completely sure the page itself is reloaded, but the other cached elements are not is to change the location.search query string with a random parameter.
完全确定页面本身重新加载但其他缓存元素不会重新加载的最佳方法是使用随机参数更改 location.search 查询字符串。
For example,
例如,
location.search='?'+Math.random();
The cached is not bypassed, so all elements of the page will be loaded from cache, but the main page itself is forced to reload because HTTP requires another request to the server for each unique query string / location.search value.
缓存不会被绕过,所以页面的所有元素都会从缓存中加载,但主页本身被强制重新加载,因为 HTTP 需要对服务器的每个唯一查询字符串/location.search 值的另一个请求。
If your page is itself an application which uses a query string (like file.cgi?param=xyz¶m2=abc...) then you will have to append to the query string rather than replace it completely:
如果您的页面本身就是一个使用查询字符串的应用程序(如 file.cgi?param=xyz¶m2=abc...),那么您必须附加到查询字符串而不是完全替换它:
if (location.search.length > 1)
location.search+='&'+Math.random();
else
location.search='?'+Math.random();
回答by Roger
I have been trying all these for the following situation: I have a complicated page with a minicart where the customer can save projects with lists of products. Now I wanted to let the customer open a new page to download a report while keeping the parent page intact with open accordion etc.
我一直在针对以下情况尝试所有这些:我有一个带有微型购物车的复杂页面,客户可以在其中保存带有产品列表的项目。现在我想让客户打开一个新页面来下载报告,同时保持父页面完整,打开手风琴等。
Customers with IE8 still running XP would have the parent page garbled after opening the report window. Programming was in ASP.NET with VB code behind. This solution worked for me.
使用 IE8 仍然运行 XP 的客户打开报告窗口后,父页面会出现乱码。编程是在 ASP.NET 中进行的,后面是 VB 代码。这个解决方案对我有用。
Dim proj As String = "<script language=javascript> location.assign('/Projects.aspx');
window.open('Report.aspx?project=" & projid.Text & "&rpt=bill');</script>"
Session.Add("AddCart", idx.ToString("N0"))
Response.Write(proj)
location.assign is used to refresh the parent page while window.open will open the report in a new window.
location.assign 用于刷新父页面,而 window.open 将在新窗口中打开报表。
A session variable is used to keep track of which accordion row should be opened. This is handled in Page_Load
会话变量用于跟踪应打开哪个手风琴行。这是在 Page_Load 中处理的
If Not Page.IsPostBack Then
LoadProjects()
If Session("AddCart") IsNot Nothing Then
Dim idx As Integer = Session("AddCart")
ProjAccordion.SelectedIndex = idx
Session.Remove("AddCart")
ElseIf Session("CopyProj") IsNot Nothing Then
Dim idx As Integer = Session("CopyProj")
ProjAccordion.SelectedIndex = idx
Session.Remove("CopyProj")
End If
End If
回答by Singagirl
Using location.href makes the trick http://www.w3schools.com/jsref/prop_loc_href.asp
使用 location.href 使技巧 http://www.w3schools.com/jsref/prop_loc_href.asp