admin管理员组文章数量:1292888
I have a scenario where a user when clicks on a link, they are directed to a page in which I want to add a code to fetch a variable and redirect to another page.
i.e. user clicks on <a href="sample.tpl">click here</a>
on sample.tpl I want to write a code to redirect him to another page
<script>
window.location="/?page_id=10"
but I want to send a variable too on this new link without appending it to the URL for security reasons.
How can I do it with some safe procedure?
Do ask me questions if it is not clear.
I have a scenario where a user when clicks on a link, they are directed to a page in which I want to add a code to fetch a variable and redirect to another page.
i.e. user clicks on <a href="sample.tpl">click here</a>
on sample.tpl I want to write a code to redirect him to another page
<script>
window.location="http://example./?page_id=10"
but I want to send a variable too on this new link without appending it to the URL for security reasons.
How can I do it with some safe procedure?
Do ask me questions if it is not clear.
Share Improve this question edited Feb 13, 2023 at 13:21 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 8, 2012 at 8:13 atifatif 1,69313 gold badges38 silver badges70 bronze badges 5- Did you try saving that in a cookie then accessing it in the other page ? – Issa Qandil Commented Oct 8, 2012 at 8:14
- Is the target page yours or not? What you mean by security reasons? – Michal Klouda Commented Oct 8, 2012 at 8:15
- yes target page is mine, and for cookies what if cookies are disabled – atif Commented Oct 8, 2012 at 8:17
- It may help if you explain exactly what you are trying to do and why, there may be a better plete solution. Not much is truly secure in the webworld, unless you're using a form over SSL. – Dale K Commented Oct 8, 2012 at 8:20
- actually i want only the registered users to see that page, but that user authentication es from another database. so i don't want it to be appendid to url as anyone will e and access the page – atif Commented Oct 8, 2012 at 8:20
2 Answers
Reset to default 4You could create a form with method="post"
, a hidden
input with the value you want to pass and a submit
button styled as a regular link (if you want to also manually send the form).
Then just submit the form manually or programmatically through the submit()
method
Example (with automatic redirect after 3 seconds after page load) http://jsbin./avacoj/1/edit
Html
<form method="post" action="http://mydomain./" id="f">
<input type="hidden" name="page_id" value="10">
<noscript><button type="submit">Continue</button></noscript> /* see below */
</form>
Js
window.onload = function() {
var frm = document.getElementById('f');
setTimeout(function() {
frm.submit();
}, 3000);
};
As a side note you may consider to insert a submit
button inside <noscript></noscript>
tag so the redirect will be possibile even when js is not available on the user device, so the page is still accessible.
Further to Fabrizio's answer someone has written a javascript function which will allow you to build the form and send it via POST
at runtime.
POST
is like GET
(Where the variable is appended to the url) except the variable is sent via the headers. It is still possible to fake a POST
request so you must perform some kind of validation on the data.
function post_to_url(path, params, method) {
method = method || "post"; // Set method to post by default, if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
Used like so:
post_to_url("http://mydomain./", {'page_id':'10'}, "post");
Source: JavaScript post request like a form submit
本文标签: javascriptHow can I send the data to another page without appending it in a URLStack Overflow
版权声明:本文标题:javascript - How can I send the data to another page without appending it in a URL? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741567302a2385801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论