使用JavaScript和GET变量刷新页面

3
<script type="text/javascript">
var email = document.write(localStorage.getItem('email'));
var pass = document.write(localStorage.getItem('pass'));
var url = document.write(document.URL);
document.location.href = url+"?email="+email+"&pass="+pass;
</script>

但是当我进入页面时,我的url看起来像这样:http://example.com/undefined?email=undefined&pass=undefined 不起作用... 有人知道问题在哪里吗?非常感谢!
1个回答

1

那么,这里的 document.write(…) 是怎么回事呢?你不想打印出任何东西:

var email = localStorage.getItem('email');

但是如果你想打印出测试值:
var email = localStorage.getItem('email');
document.write(email);

(参见console.log(…)

您应该使用encodeURIComponent(…)转义参数:

location.href = url + "?email=" + encodeURIComponent(email) +
                "&pass=" + encodeURIComponent(pass);

你不应该使用document.write来改变网站内容,有更合理的方法可以动态修改内容。

不要使用GET请求发送密码,因为它们会出现在浏览器、代理和服务器日志中。请使用POST请求通过隐藏表单发送。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接