So, quick question - I check to see if a checkbox is clicked on an html doc and then when the user clicks on a continue button, it takes them to the next page, which will do stuff based on whether or not that checkbox was checked. What's the easiest way of doing this?
In the index.html file (homepage) I have
if ($("#checkArray").is(":checked")) {
console.log("permission granted");
} else {
console.log("No permissions granted");
}
window.location='/auth/facebook';
which checks to see if the check box is marked. it then goes to the next html document (confirmation.html) for authorization of facebook here, I want to call a function if the checkbox was checked (true).
Please help! Thanks!!
-
2You can add a query parameter to the URL if the checkbox was checked, and test for its existence on the other page.Felix Kling– Felix Kling2014年08月10日 23:22:40 +00:00Commented Aug 10, 2014 at 23:22
-
That would be pretty easy to just mess with I'd think FelixScott Mitchell– Scott Mitchell2014年08月10日 23:23:40 +00:00Commented Aug 10, 2014 at 23:23
-
Do you think its wise to seperate this checkbox stuff in another page thats used for authentication?Scott Mitchell– Scott Mitchell2014年08月10日 23:24:18 +00:00Commented Aug 10, 2014 at 23:24
2 Answers 2
You can use cookies or sessionStorage.
Example with SessionStorage:
if ($("#checkArray").is(":checked")) {
window.sessionStorage.setItem('checked', true);
} else {
window.sessionStorage.setItem('checked', false);
}
window.location='/auth/facebook';
And on other page:
var checked = window.sessionStorage.getItem('checked');
SessionStorage works with modern browsers so if you need to support old browsers like IE8, you should use cookies. You can read more about cookies here: http://www.w3schools.com/js/js_cookies.asp.
Comments
Following Felix Kling
Try
at index.html
$("input[type='button']").on("click", function(e) {
var checked = $("input[type='checkbox']").prop("checked");
if (checked) {
// do stuff
// if `_checked` === true
console.log(checked);
}
else {
// do stuff
// if `_checked` === false
console.log(checked)
};
var conf = window.location.href;
window.location.href = conf + "?checked=" + checked;
});
at confirmation.html
$(window).on("load", function(e) {
var _checked = (e.target.location.href
.split("?").slice(1,2)[0].split("=")[1] === "true"
? true
: false
);
if(_checked) {
// do stuff
// if `_checked` === true
console.log(_checked);
} else {
// do stuff
// if `_checked` === false
};
});