I have a tabbed navigation menu and I have created hashed URLs for each tab. In one tab I have multiple information cards and a check box in the top right hand corner of each card. when this check box is checked the card's background color changes.
My objective is that when the page is refreshed or sent as a link the cards that have been checked remain checked along with the changed background color. Any help would really be appreciated.
-
2Just generate a string: newURL= URL + "?param1=this¶m2=that"durbnpoisn– durbnpoisn2016年01月15日 18:07:32 +00:00Commented Jan 15, 2016 at 18:07
-
you can use localStorage. ex: on every check box selection, store current-tab in localStorage like...localStorage.setItem('current-tab', 'tab1'). on document ready you can get same thing using localStorage.getItem('current-tab'), it will return 'tab1', then you can manipulate urlsaikumar– saikumar2016年01月15日 18:10:55 +00:00Commented Jan 15, 2016 at 18:10
-
@saikumar But that won't work if it's sent as a link.Mike Cluck– Mike Cluck2016年01月15日 18:11:44 +00:00Commented Jan 15, 2016 at 18:11
-
yes, then first comment is usefullsaikumar– saikumar2016年01月15日 18:15:09 +00:00Commented Jan 15, 2016 at 18:15
-
I agree the first comment is very useful. . . but call me an idiot, I have no idea how to do that. :PAbout Leros– About Leros2016年01月15日 18:16:58 +00:00Commented Jan 15, 2016 at 18:16
2 Answers 2
You can use a function like this:
function dataToQueryString(data){
var encoded = [];
for(var key in data){
// skip prototype properties
if(!data.hasOwnProperty(key)) continue;
// encode string properly to use in url
encoded.push(key + '=' + encodeURIComponent(data[key]));
}
return '?' + encoded.join('&');
}
And use it like:
data = {
id: 1,
name: "John Doe"
}
url = url + dataToQueryString( data );
Or more specific for your case:
function storeCardsInQueryString(cards){
var encoded = [];
for(var i = 0; i < cards.length; i++){
encoded.push('cards[]=' + cards[i]);
}
return '?' + encoded.join('&');
}
And use it like:
cards = [ 1, 7, 21, 22, 53 ];
url = url + storeCardsInQueryString( cards );
5 Comments
key in the string... Or is that not what you mean?I think you need to use the combination of url parameter and localStorage to achive both refreshing and link requirements.
Lets say a user open your site with this url:
www.mysite.com/mypgae?tab=tab1&card1=checked&card2=unchecked&card3=checked
You read these parameters in your page javascript and will goto tab1 and mark card2 and card3 as checked and card2 as unchecked
now lets assume that the user changes the tab and some cards, you should store these changes in the localStorage and next time the user refreshed the page, at first you check the localStorage if it was empty you go for the url. But, if localStorage was not empty you give priority to localStorage and apply changes based on localStorage instead of url.
That is the only way that comes to my mind to support both url and local changes at the same time.
1 Comment
Explore related questions
See similar questions with these tags.