There are several ways to do this, and this is related to how you hide or upload your images.
1. simple method
if you donβt need the user age and you just need to switch, you can do this with just the js variable, cookie and two versions of the link. however, you do not hide images, but download them. filtering is done on the server, where you can use a database query or a simple split of folders. eg:
var nsfw = read_cookie('nsfw', false); // not an actual js function, search for how to read cookie in js --- read cookie value, default to false function loadImage(nsfw){ if (nsfw){ $.get('nsfw-image-list-url', function(resp){ // the url should return a json with list of image urls var list = resp; // jQuery automatically parse json with the right MIME list.forEach(function(val){ // insert image to page $('#container').append('<img src="' + val + '/>'); }); }); } else { $.get('sfw-image-list-url', function(resp){ // the url should return a json with list of image urls var list = resp; // jQuery automatically parse json with the right MIME list.forEach(function(val){ // insert image to page $('#container').append('<img src="' + val + '/>'); }); }); } }
and in the button click event:
nsfw = !nsfw; // clear the image first if needed $('#container').empty(); loadImage(nsfw);
2. another simple method, but not as simple as # 1
you can also do this with just one link, which returns a list of images with their type, for example nsfw or other things.
note: this method still uses cookie
for example, the returned list is as follows:
[ {"url": "some-image-1.jpg", "nsfw": "true"}, {"url": "some-image-2.jpg", "nsfw": "false"}, {"url": "some-image-3.jpg", "nsfw": "true"}, {"url": "some-image-4.jpg", "nsfw": "false"}, {"url": "some-image-5.jpg", "nsfw": "false"}, {"url": "some-image-6.jpg", "nsfw": "true"} ]
then you just visualize it when the conditions are met.
function renderImage(nsfw){ $.get('image-list-url', function(resp){ list.forEach(function(val, key){ if (nsfw || !val.nsfw){ $('#container').append('<img src="' + val.url + '/>'); } }); }); }
and many other methods that take too long to explain, such as using Angular, React or Vue
still uses cookies to reload or back and does not take into account the age of the user.
as for a session-based approach, you only need this if you need to check users age
that is, if you have a membership function with DOB (date of birth) data on your site, if so, you can use @KScandrett's answer