What I am looking to achieve is that when a user comes to my site they immediately encounter a pop up (or a landing page) that asks, are you a man or a woman or other (choose x or y or z). They pick and click the link, then once they choose they go to that part of the site (and no it isn't a adult site ;). A cookie then remembers their choice and each time they return they are automatically redirected to their choice.
So basically we need the ability to set a cookie based on their choice (via link or form button) and then when they return to the homepage get that cookie and redirect.
I've seen some questions and answers that are close but just not quite there with this scenario and I end up confused.
What do you think? Doable? Please help and thanks so much.
Without knowing much of your architecture, I came up with the following:
This code should be called after the user has made the choice:
//´choice´ contains the value of what the user chose
//Setting the cookie:
document.cookie="gender="+ choice +"; expires=Thu, 18 Dec 2029 12:00:00 UTC";
//Redirect the user after the cookie has been set:
document.location.href = "http://yoursite.com/";
Take a look how cookies are defined in PHP: http://php.net/manual/en/function.setcookie.php
Then in PHP code you may read what cookies are set when the user loads your site:
if($_COOKIE["gender"] === "x"){
//Do some x stuff
} else if($_COOKIE["gender"] === "y") {
//Do some y stuff
}
Related
Not sure if this can even be done using cookies but I feel I am close using my current code. I have a multi language site (english and french) and I'm trying to set a cookie when the user "logs in" which sets a cookie on success of log in. If the user has never logged in and try's to access another page in the site it redirects them to the english "log in" page as of now. Problem is if they try and access a french page it still redirects them to the english "log in". I'd like it to redirect them to the language they are trying to access if that makes any sense. All the french pages have the same name as the english pages but with an added "-fr.html" so "index-fr.html" is the french index and so forth.
this is close but it is creating a bad loop
jQuery:
cookie set on ajax success: _siteNS.Utils.setCookie("x-access", true, 365);
function checkCookie() {
var current_url = window.location.pathname;
var logInCookie = _siteNS.Utils.readCookie("x-access");
//if the cookie is false and location is not the landing page set location to landing page
if (!logInCookie && current_url != "/landing.html") {
window.location.replace("/landing.html");
}
if (window.location.href.indexOf("-fr.html") > -1 && !logInCookie) {
window.location.replace("/landing-fr.html");
}
}
I'm working on a project and I have to make a button to remember users selected specific page. It should work like a cookie, but I need some more information about how to save website page on cookies. The main part is when users clicks on a button, website remember users selection and when users enter after some time the same page, he will see his selection. And of course, if user push the button one more time, his selection deletes.
P.s. I'm new guy in coding so, your help will be useful for me.
Thanks in advance.
You can use browser storage for this
When user select some value:
localStorage.setItem("userSelection", "selection-value");
And when the user enter on page, you verify if userSelection exists:
var userSelection = localStorage.getItem('userSelection');
if ( userSelection ) {
// some action
}
See more: localStorage
Use any of these below to store user preferences
sessionStorage.setItem("sessionData", "I am set in session storage.");
localStorage.setItem("localData", "I am set in local storage.");
My recommendation is to store preference using URL hash combination while.storing in session or localstorage
you can do that based on ids like in my example right bellow
function getCookie(name){
var b=name+"=",
c=decodeURIComponent(document.cookie),
d=c.split(";");
for(var e=0;e<d.length;e++){
for(var f=d[e];" "==f.charAt(0);)f=f.substring(1);
if(0==f.indexOf(b))return f.substring(b.length,f.length)
}
}
function setCookie(name,value,period,path){
var time=new Date;
time.setTime(time.getTime()+1e3*(60*(60*(24*period))));
document.cookie=name+"="+value+";"+"expires="+time.toUTCString()+";path="+(path?path:"/")
}
function removeCookie(cookie_name){
if(getCookie(cookie_name)){
document.cookie=cookie_name+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}
}
document.getElementById('xxx').onclick = function(){
if(getCookie(this.id)==1) {
removeCookie(this.id)
} else {
setCookie(this.id,1,999999);
}
}
<button id="xxx">Selection 1</button>
and of course use php or maybe javascript to detect if cookie exists.
Explination:
We are facing some issues with logged in users being able to checkout via wordpress's woocommerce. The fix for these few users that reached out to us was to simply clear their cookies. (Because we recently changed servers which mucked things up a bit causing this checkout issue)
So i want to provide a red bar at the top of our checkout and cart pages that recommends to our users that they update the cookies to our site. And provide a button they can click that will remove the cookie for them.
Problem:
Okay so i have this script.. Which I am told is suppose to work.
Start your bacon
--
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
function clearAndRedirect(link) {
deleteAllCookies();
document.location = link;
}
Start your bacon
However when i open dev tools and select the application tab. I am able to clear my cookie by simply right clicking and selecting clear - at which point it deletes itself and removes itself from the cookie folder.
But when I am in dev tools and use this button script to clear my cookie. The cookie remains in the file unlike if i cleared it manually by right clicking on it in dev tools. So I am unsure if its working or not. How do i properly test this?
Also how do i make it re-direct the user to the login page after the cookie is successfully cleared and successful? instead of just directly after the function runs.
Any help much appreciated. Not sure if its working.
I ve made a mobile version of a website. Before the user see a specific page he must press a button - CONFIRM, which basically confirms that he accepts the terms of our website. Moreover, as soon as the user press the CONFIRM button he is being redirected to the specific page.
My question is how is it possible to use cookies or localStorage in order to redirect the users who already had been accepted the terms to the specific web page?
I am not quite programming savvy so please be nice :)
You can check like this:
if (localStorage.getItem('accepted') === '1') {
//redirect - already accepted;
}
// after clicking accept
if (accepted === true) {
localStorage.setItem('accepted', '1');
//redirect
}
Note that the check and set must be in the same origin.
Demo:
http://jsfiddle.net/AbdiasSoftware/wk9bS/
Of course, you'll need to check that localStorage is available (implicit IMO) and provide a fallback mechanism such as persistent cookies:
if (!!window.localStorage) {
//use code above
} else {
//use fallback
}
Situation looks like that:
I need to have button on my site that will link to subpage with video.
There are two subpages - one with high quality video and second with low quality video.
When somebody click on button first time then it redirect him to subpage high quality video. From this subpage he can switch to second subpage (with low quality video).
The problem:
I want to remember in cookies on which web page with video client was last in (low or high quality). So that when client returns to my website, button will lead him to page with video that he was last in.
I use ASP.NET MVC 2. But I think that solution to this problem is probably some javascript.
Any help here much appreciated!
Cookies are passed to the server with each HTTP request.
Assuming your button is generated dynamically on the server, you can inspect the incoming cookies to see if the user has the parameter in question set to low quality and update the button URL accordingly.
ASP docs
From experience with ASP.Net WebForms, its pretty straightforward to access cookies and I am pretty sure things are setup similarly w/ MVC.
String GetBandwidthSetting()
{
HttpCookie bandwidth = Context.Request.Cookies["bandwidth"];
return (bandwidth != null) ? bandwidth.Value : null;
}
String SetBandwidthSetting(String value)
{
HttpCookie bandwidth = new HttpCookie("bandwidth", value);
bandwidth.Expires = DateTime.Now.AddYears(1);
Context.Response.Cookies.Add(bandwidth);
}
You can check this script:
http://javascript.internet.com/cookies/cookie-redirect.html
It is similar to what you need.
In .js you have to change last if statement to one looking similar to that:
if (favorite != null) {
switch (favorite) {
case 'videohq': url = 'url_of_HQ_Video'; // change these!
break;
case 'videolq': url = 'url_of_LQ_Video';
break;
}
And then add this to button/link:
onclick="window.location.href = url"
to your site on which you are redirectiong to those videos.
Remember also to add code that set cookies. You can add action similar to this:
onClick="SetCookie('video', 'videohq' , exp);