last day (14 days later) » 

11:34 AM
0
A: save a favorite page in collection list based on cookies or local storage

ArshYou can do this with javascript and jQuery easily. use the below code to add to favourite the page that user is on and save it inside cookies and then fetch the list of pages saved as favourite. i have made you a full file with which you can play around , inside the file i have created the funct...

 
thank you very much for your answering i have problem in understanding this code could you please explain me a little about functions you used again thank you
for example what this function do exactky function createCookie(name, value, days)
 
@user6362236 createCookie , makes cookie with specified name inside user's browser , or if the cookie with that name is already there , then it will update the cookie and new value that we have added. I have made comment on function to let you now , what this function is doing
@user6362236 check my updated answer for detailed information about the main functions.
 
thank you very much for your explanations. i click on add me to fav but nothing happens which page this favorite results save. i mean i think i have to specify a Destination page which my favorite page go and save there?
 
This will add to fav the page you are on with it's url.
 
hi, about my last question i have a page like (localhost/responsiveviewmore.php?ID=139) in my website i add your code to the page now i click on add to favorits . now the question is where this url save am i have to create a favorite page for my website?
 
12:04 PM
i mean how can i fetch the list of pages saved as favourite.
 
you can place the javascript code inside a javascript file and include it in any page , where you want to make available the add me to fav option ,
then for fetching the list
you have to add :

<ul id="appendFavs">


</ul>
inside this ul list the list elements will be created
this is a part of code that creates a list of pages inside ul
var myfaves = JSON.parse(readCookie('favespages'));
faves = myfaves;
$.each(myfaves,function(index,value){
var element = '<li class="'+index+'"><h4>'+value.title+'</h4> <a href="'+value.url+'">Open page</a> '+
'<a href="javascript:void(0);" class="remove" data-id="'+index+'">Remove me</a>';
$('#appendfavs').append(element);
});
as you can see ; $('#appendfavs').append(element);
this is targeting the ul element 's id to append list elements in it. You can customize the layout as your want , just remember to place the id's and target the correct id
fav list of pages will be added where-ever the
<ul id="appendFavs">
is located
____________________________________________________________
var myfaves = JSON.parse(readCookie('favespages'));

the above code will return you a list of fav pages in json array format, which you can use anywhere you want
if my answer did help you , feel free to accept it . thanks
 
12:31 PM
yes you really helped me but i do not know why the code does not work. i exactly copy your code as favorite.html without any change and when i run, it shows me a link as "Add me to fav" when i click on it nothing happens no open page or remove me as you write in code or show in image just nothing i do not know why?
please it is really necessary for me if you can check it yourself and tell me results thank you a lot
 
Hi , please check your console
to see if you see any error
 
there is not any error.
 
your browser's cookies are on
?
 
how can i know it is on?
 
which browser are you using?
 
12:39 PM
google crome
 
i checked it is on
 
did you change anything in the code?
because strange , it should work without any problem
 
let me copy and past it one more time
 
yeah
make sure you are loading the jquery library correctly from google apis
make your your internet connection on your localhost is working correctly
 
12:54 PM
i checked again still no answer
 
please show me browser's screenshot
and code screenshot , inside your file.
 
i edit you can see:
http://stackoverflow.com/questions/37831446/save-a-favorite-page-in-collection-list-based-on-cookies-or-local-storage
 
Please use the code inside localhost
it will not work like that
you have to use the code inside localhost , which is virtual server , copy your html file and paste it inside localhost folder , then try to run it
 
1:13 PM
i checked it and edit my previous image still nothing happen
now i clicked on run code snippet it gives an error you can check it yourself
 
That code error is specified with reason , it is a sandbox error , which doesn't allow cookies.
 
1:47 PM
copy and paste the code below inside script tags inside head, remember to delete the old javascript code
function createCookie(name, value, days) {
var expires = '',
date = new Date();
if (days) {
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = '; expires=' + date.toGMTString();
}
document.cookie = name + '=' + value + expires + '; path=/';
}
/*
* Read cookie by name.
* In your case the return value will be a json array with list of pages saved.
*/
function readCookie(name) {
var nameEQ = name + '=',
allCookies = document.cookie.split(';'),
i,
cookie;
for (i = 0; i < allCookies.length; i += 1) {
check this jsfiddle
i did create jsfiddle to try for you
 
2:07 PM
I really appreciate your time still i can not get result.
one question if i try jsfiddle and press add, is it gonna show me result because i tried and i did not get result there too.
 
2:22 PM
i do not get it how can it possible i run a codr and it does not work and the same code work for you. i am confuesed
 

last day (14 days later) »