硬刷新浏览器页面,然后使用 javascript 重定向到 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29666798/
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
hard refresh of browser page and then redirect to a url using javascript
提问by Harshit Laddha
I am trying to reload the page and redirect it to another one, or just redirect it to another one but I can't do that by
我正在尝试重新加载页面并将其重定向到另一个页面,或者只是将其重定向到另一个页面,但我不能这样做
window.location.href
or
或者
windown.location
as I have an Angular Single Page app which catches the route and instead of reloading the page opens the partials directly
因为我有一个 Angular Single Page 应用程序,它可以捕获路线,而不是重新加载页面,而是直接打开部分
I did try -
我确实尝试过-
window.location.reload(true)
but it reloads the page and cannot change the url I have also tried -
但它重新加载页面并且无法更改我也尝试过的网址 -
window.location.assign
and
和
window.location.replace
Is there any way to do this?
有没有办法做到这一点?
回答by Reena
There are three cases where AngularJS will perform a full page reload:
AngularJS 会在三种情况下执行整页重新加载:
Links that contain target element
包含目标元素的链接
Example:
例子:
<a href="/ext/link?a=b" target="_self"> link </a>
Absolute links that go to a different domain Example:
转到不同域的绝对链接示例:
<a href="http://angularjs.org/">link</a>
Links starting with '/' that lead to a different base path when base is defined Example:
定义 base 时,以“/”开头的链接会指向不同的基本路径 示例:
<a href="/not-my-base/link">link</a>
Updated:
更新:
Using javascript:
使用javascript:
The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: $window.location.href.
$location 服务只允许更改 URL;它不允许您重新加载页面。当您需要更改 URL 并重新加载页面或导航到其他页面时,请使用较低级别的 API:$window.location.href。
See:
看:
https://docs.angularjs.org/guide/$location
https://docs.angularjs.org/guide/$location
回答by Pankaj Parkar
For redirect to different page you should use
要重定向到不同的页面,您应该使用
$window.open('url', '_self')
Which will load your page again.
这将再次加载您的页面。
回答by Collin Grady
So you need to change the hash on the end of the URL? i.e. from #/
to #/otherpage/
?
那么您需要更改 URL 末尾的哈希值吗?即从#/
到#/otherpage/
?
If so, location.hash = "/otherpage/";
如果是这样, location.hash = "/otherpage/";
回答by Adarsh Hegde
try
window.location = url;
尝试
window.location = url;
do remember that the url must have a protocol i.e http:// or https://
请记住,该 url 必须有一个协议,即 http:// 或 https://
回答by CJ Dennis
According to https://docs.angularjs.org/guide/$location, if AngularJS is using HTML5 mode, it never performs a full page reload in a modern browser.
根据https://docs.angularjs.org/guide/$location,如果 AngularJS 使用 HTML5 模式,它永远不会在现代浏览器中执行完整页面重新加载。
HTML5 mode
In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents. If the HTML5 History API is not supported by a browser, the $location service will fall back to using the hashbang URLs automatically. This frees you from having to worry about whether the browser displaying your app supports the history API or not; the $location service transparently uses the best available option.
Opening a regular URL in a legacy browser -> redirects to a hashbang URL Opening hashbang URL in a modern browser -> rewrites to a regular URL Note that in this mode, Angular intercepts all links (subject to the "Html link rewriting" rules below) and updates the url in a way that never performs a full page reload.
HTML5 模式
在 HTML5 模式下,$location 服务的 getter 和 setter 通过 HTML5 历史 API 与浏览器 URL 地址交互。这允许使用常规 URL 路径和搜索段,而不是它们的 hashbang 等效项。如果浏览器不支持 HTML5 History API,$location 服务将自动回退到使用 hashbang URL。这让您不必担心显示您的应用程序的浏览器是否支持历史 API;$location 服务透明地使用最佳可用选项。
在旧浏览器中打开常规 URL -> 重定向到 hashbang URL 在现代浏览器中打开 hashbang URL -> 重写为常规 URL 请注意,在这种模式下,Angular 会拦截所有链接(遵循下面的“Html 链接重写”规则) ) 并以从不执行完整页面重新加载的方式更新 url 。
(Emphasis mine)
(强调我的)
Since Hashbang mode is the default, the code must be setting HTML5 mode somewhere.
由于 Hashbang 模式是默认模式,因此代码必须在某处设置 HTML5 模式。