Refresh the page with javascript and GET variables

三世轮回 提交于 2019-12-10 10:28:24

问题


<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>

But when I enter the page I left the url like this: http://example.com/undefined?email=undefined&pass=undefined

Not happening ... Anyone know the problem? Thank you very much!


回答1:


Well, what's up with document.write(…) in here? You don't want to print out anything:

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

But if you want to print out the values for testing:

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

(See also console.log(…))

You should escape the parameters using encodeURIComponent(…):

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

Also you should not use document.write anyhow. There are plenty more reasonable methods to change the content dynamically on you website.

You should not send a password using GET requests, as they will appear the browser, proxy and server logs. Use POST requests through invisible forms.



来源:https://stackoverflow.com/questions/17253754/refresh-the-page-with-javascript-and-get-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!