Javascript 清除url而不清除页面变量
我有一个我尝试使用的网站:Javascript 清除url而不清除页面变量,javascript,php,Javascript,Php,我有一个我尝试使用的网站: <script type="text/javascript"> $(window).bind("load", function() { var url = window.location.href; if ( url.split('?').length >= 2 ) { window.location = url.split("?")[0]; } });
<script type="text/javascript">
$(window).bind("load", function() {
var url = window.location.href;
if ( url.split('?').length >= 2 ) {
window.location = url.split("?")[0];
}
});
</script>
$(窗口).bind(“加载”,函数(){
var url=window.location.href;
如果(url.split(“?”).length>=2){
window.location=url.split(“?”[0];
}
});
清除url(website.com?id=12313)。但在网站上,我也有一个读取此id的PHP脚本。甚至可以使用上述脚本,并确保PHP Get变量在清除URL之前读取它。PHP将在javascript运行之前运行很长时间,因此PHP没有读不到它的风险。但是,当您使用window.location更改url时,您将重新加载页面并再次点击php,因为您删除了它,所以它将无法找到id 正确的处理方法是通过服务器端url重写。对于apache,它通常通过
.htaccess
完成,对于IIS7,它通过web.config
通常,您不想完全删除id,只需将其清理干净即可
http://foo.com/12313
如果完全删除id,书签将无法工作(从php获取id变得非常困难)
另一种方法是使用历史api来更改url,而不会导致页面刷新
使用.htaccess可能是一种方法
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?id=$1 [QSA,L] #rewrites all urls
您是否考虑将变量存储在Cookie中?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?id=$1 [QSA,L] #rewrites all urls