下面的代码效果很好.这是我的问题:窗口网址重定向,但原始网址未记录在我的浏览器历史记录中.
例如,如果我访问"http://example.com/page1",浏览器就会重定向到"http://example.com/test".但是,我需要访问的原始网址("http://example.com/page1")显示在我的浏览器历史记录中,以便我可以在其他功能中调用它.
在重定向之前,是否有访问原始网址以登录浏览器的历史记录?
<!-- script to enable age verification cookies and ensure people have age checked -->
<script type="text/javascript">
$(document).ready(function(){
if (window.location =="http://example.com/home") {//do nothing
} else {
window.location = "http://example.com/test";
}
});
</script>
Run Code Online (Sandbox Code Playgroud) 我使用此方法导航到url,触发事件而不是将url推送到浏览器历史记录.但是Backbone.history.navigate(url,{trigger:true, replace: true})从历史中替换以前的URL.示例:浏览器hystory
之前
localhost:port/url
Backbone.history.navigate(url + '/list',{ trigger:true, replace: true })
预期:路由触发事件url + '/list'和浏览器历史记录localhost:port/url
实际上:触发事件但浏览器历史记录localhost:port/#url/list.以前的网址被替换
我有一个站点的测试版本位于普通站点的子域,如:http: //test.x.com而不是http://x.com.
我使用<base>标记将所有资源请求转换回原始域:
<base href="http://x.com/" />
Run Code Online (Sandbox Code Playgroud)
在我实现HTML5 push/replaceState支持之前,这种策略很有效.
现在,如果我在控制台中执行此语句:
history.pushState({}, "", "");
Run Code Online (Sandbox Code Playgroud)
...然后我DOMException在基于WebKit的浏览器中获得一个对象:
code: 18
constructor: DOMExceptionConstructor
line: 2
message: "SECURITY_ERR: DOM Exception 18"
name: "SECURITY_ERR"
sourceId: 4839191928
__proto__: DOMExceptionPrototype
Run Code Online (Sandbox Code Playgroud)
...和FireFox 4中的此错误:
Security error" code: "1000
Run Code Online (Sandbox Code Playgroud)
如果我删除<base>标记并执行相同的语句,则会推送新状态,并且没有异常.
一些问题:1)这种行为是安全风险,还是一个bug?并且2)是否有一种解决方法来防止异常,或者除了使用<base>将完全回避问题的标签之外的策略?
谢谢你的考虑.
说我做以下事情:
我需要做到这一点,以便用户返回主页(/)而不是返回/ posts/1
所以我需要允许骨干哈希路由工作,但不能修改历史记录.我个人更喜欢保留历史,但这是项目的要求.
在尝试直接访问window.location或window.history时,如何在AngularJS中执行以下操作:
例如:
我正在创建一个应用程序,我登录/注销有一个家,关于,配置文件等.它使用反应路由器,但是当我登录时,我希望应用程序转换到它所在的最后一条路线,它有时适用于例如,如果我访问,然后登录并登录,它会带我回到约,但如果我访问应用程序根目录'/'然后去登录,并登录,它会带我回到我曾经在的第一页(当您打开浏览器或选项卡时,这将是默认页面).我这样写了,我觉得我这样做的方式非常幼稚.我在渲染方法中做到了,它被称为很多.我认为它有时会这样做,但我不完全确定.希望得到一些建议,代码:
constructor(props, context) {
super(props, context);
this.state = UserStore.getState();
this.context = context;
}
render() {
console.log(this.state.user.get('authenticated'));
if(this.state.user.get('authenticated')){
this.context.history.goBack();
}
return (
//stuff
)
Run Code Online (Sandbox Code Playgroud)
我是否应该在我访问的每条路线上将状态推入历史记录中,我还注意到,如果我在登录之前等待一段时间,有时它会正确执行,也许它会将其添加到历史记录中稍晚或需要一些时间?不完全确定如何调试它,console.log(this.context.history)只是显示了它的一些功能,我想我可能会在错误的地方看?
路线:
export default (
<Route component={App}>
<Route path="/" component={Dashboard} />
<Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
<Route path="about" component={About} />
<Route path="profile" component={Profile} onEnter={requireAuth}/>
<Route path="login" component={LoginSignupPage} />
<Route path="channels" component={Channels} />
</Route>
);
Run Code Online (Sandbox Code Playgroud)
注入反应路由器
import React from 'react';
import ReactDOM from 'react-dom';
import Iso from 'iso';
import createBrowserHistory from 'history/lib/createBrowserHistory';
import { Router } from 'react-router'; …Run Code Online (Sandbox Code Playgroud) 在 Safari 中,当replaceState 调用超过 100 次时,会抛出:
SecurityError:DOM 异常 18:尝试突破用户代理的安全策略。
更多信息: https: //forums.developer.apple.com/thread/36650
我的问题是,在某些特定条件下,我会在用户滚动时更改 URL(使用 )$(window).scroll(function() {...。正如您可能猜到的那样,我在不到 2 秒的时间内达到了 100 的限制。
history.replaceState({}, '', newStringWithURLToUpdateInClientBrowser);
有什么解决方法吗?现有的允许管理的库history可以解决这个问题吗?
所有其他浏览器都不受此问题的影响。只有Webkit。这是查看错误的小提琴: https: //jsfiddle.net/j1sxxLwy/
在 Chrome 中它将达到 100,但请尝试在 Safari 中运行它。
我有Android Web View的问题.当我覆盖函数shouldOverrideUrlLoading在我的类中扩展WebViewClient然后WebView.canGoBack()总是返回false.
以下是我的代码
public class SMWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String uri) {
view.loadUrl(uri);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的活动中:
@BindView(R.id.main_webview) SMWebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
webView.setWebViewClient(new SMWebViewClient(){
@Override
public void onPageFinished(WebView view, String url){
}
});
webView.clearCache(true);
webView.clearHistory();
webView.loadUrl(Constant.baseUrl + Constant.homeUrl);
}
Run Code Online (Sandbox Code Playgroud)
问题是函数webView.canGoBack(总是在我的活动中)总是返回false:
@Override
public void onBackPressed() {
//ALWAYS FALSE
if …Run Code Online (Sandbox Code Playgroud) 单击按钮时,我通过执行更改了 URL history.push()
import createHistory from 'history/createBrowserHistory'
const history = createHistory()
.
. some code
.
history.push(`/share/${objectId}`)
Run Code Online (Sandbox Code Playgroud)
并希望该RouteURL 中提到的组件能够呈现,但这并没有发生。但是,刷新该组件时会按预期呈现。但是我不明白为什么它不会在 URL 更改时立即呈现。我试过将组件包装在withRouter.
import React, {Component} from 'react'
import {BrowserRouter, Router, Route, Switch, withRouter} from 'react-router-dom'
import createHistory from 'history/createBrowserHistory'
import Home from './pages/home'
import ViewFile from './pages/view-file'
const history = createHistory()
class App extends Component {
constructor(props) {
super(props)
}
render() {
return (
<BrowserRouter>
<Switch>
<Route exact path={'/'} component={Home}/>
<Route path={'/share/:id'} component={withRouter(ViewFile)}/>
</Switch>
</BrowserRouter>
)
} …Run Code Online (Sandbox Code Playgroud) 我有一个URL /books,其中列出了一些书,还有一个URL模式/books/{some-book-title},它显示了书的元数据的视图/更新形式。这是一个典型的用户案例,其中用户纠正了不正确的作者条目:
GET /books
<a href="/books/ulysses">Ulysses</a>
<a href="/books/don-quixote">Don Quixote</a>
GET /books/ulysses
<form action="/books/ulysses" method="post" enctype="application/x-www-form-urlencoded">
<input name="author" value="Jamessss Joycessss">
</form>
POST /books/ulysses FORMDATA author=James+Joyce
Redirect 303 SEE OTHER location = /books/ulysses
GET /books/ulysses
<form action="/books/ulysses" method="post" enctype="application/x-www-form-urlencoded">
<input name="author" value="James Joyce">
</form>
Run Code Online (Sandbox Code Playgroud)
问题在于历史记录堆栈现在
/books/books/ulysses/books/ulysses因此,当用户按下“后退”时,他们不会返回到/books,而只是刷新当前页面。是否有非JavaScript方法来处理更新案例,以便后退按钮具有预期的行为?如果没有,那么解决此问题的JavaScript方法是什么?
编辑:如果您多次发布,则更加令人困惑
GET /books
GET /books/ulysses author=Jamesss Joycesss
POST author=3 -> redirect shows author=3
POST author=2 -> redirect shows author=2
POST author=1 -> redirect shows author=1
Press back button …Run Code Online (Sandbox Code Playgroud) browser-history ×10
javascript ×4
backbone.js ×2
react-router ×2
reactjs ×2
android ×1
angularjs ×1
history.js ×1
html5 ×1
ios ×1
java ×1
jquery ×1
macos ×1
redirect ×1
safari ×1
url ×1
webview ×1