I have some javascript on my website that loads a modal on a specific page. It uses a cookie to determine whether someone has seen the modal. I need to set an expiry date/time for this but don't know how. I've checked on here for answers but can't see anything that helps me with the code I've got.
Can anyone help? I thought the '365' in the code specified the no. of days for the cookie to expire but it's expiring when the browser session ends.
<script type="text/javascript">
$(document).ready(function() {
if ($.cookie('MyCIEH_popup') == null) {
$('#modalLarge').modal('show');
$.cookie('MyCIEH_popup', '365;');
}
});
</script>
replace $.cookie('MyCIEH_popup', '365;'); with:
$.cookie('MyCIEH_popup', 'value', { expires: 365 });
Note: It's probably a better solution to use native js for that, see here.
Related
Can I set a time limit (for example in 8 hours you have to retype the password)?
Here's my very simple javascript:
<SCRIPT language="JavaScript">
<!--hide
var password;
var pass1="x10Kz4iz4ZvEB2wgUBA5otc1";
var pass2="x10Kz4iz4ZvEB2wgUBA5otc1";
var pass3="x10Kz4iz4ZvEB2wgUBA5otc1"
password=prompt('Please enter your password to view this page!',' ');
if (password==pass1 || password==pass2 || password==pass3)
alert('Password Correct! Click OK to enter!');
else
{
alert('Uh oh try again');
window.location="#";
}
//-->
</SCRIPT>
I have this under the head tag of my HTML text.
Thank you :)
This is not really a secure way of protecting your website because anyone can look at the source code of a page and see the Javascript along with the passwords you wrote there.
However, for learning purposes, you might want to learn about setting and getting cookies (How do I create and read a value from cookie?). You can store the time the user logged in, and next time check if it has been eight hours (just compare the time and date of last login to current login time and date).
Also, to get the current time, you should read up on the Javascript Date() function.
I want to dynamically add a class to a link on a given day of the week to display it and have the link stay visible until clicked. When clicked, the class should be removed which will then hide the link and it stays hidden until the day of the week returns.
This is what I have tried.
jQuery(function($) {
var day = new Date().getDay();
if( day == 4 ) {
$(".link").addClass("shown");
}
$(".link").click(function(e) {
e.preventDefault();
$(this).removeClass("shown");
});
});
It removes correctly when clicked but as long as the 24hr period exist, the link displays when the page reloads. What would be the correct coding to achieve the goal?
Expanding on louisik1's answer, and my comment, here's a basic example of how it could be done with cookies:
https://jsfiddle.net/L9ojth2n/1/ has the working version of this.
First, on the click, check which link it is, and store it as a cookie expiring at midnight the next day (in case you want to display links multiple days of the week):
var expires=new Date();
expires.setDate(expires.getDate()+1);
expires.setHours(0);
expires.setMinutes(0);
expires.setSeconds(0);
expires.setMilliseconds(0);
document.cookie="clickedLink"+$(".link").index(this)+"=yes;expires="+expires.toUTCString()+";path=/";
And second, when you next visualize the links, check for cookies to see if any of the links have already been clicked:
if(document.cookie.split(';').reduce(function(object,cookie){
var data=cookie.split('=');
object[data[0].trim()]=data[1];
return object;
},{})['clickedLink'+$('.link').index(this)]!="yes"){
$(this).addClass("shown");
}
Note that tech savvy users may be able to undermine this and display links multiple times in a day if they delete their cookies. It's most secure to require logins and store data on the server for specific users.
Yes, you will need to store the record of the user already clicking the link. Since the page will reload all the script and re-set all variables etc., you need to store 'long life' data by some other means, of which there are several.
Typically in the past it was done with cookies to store that data on the users' computer.
Modern browsers support Local Storage, which has a very simple syntax to store data on the users' computer, like cookies. But it does require HTML5.
Storing the information in your server code somehow (such as saving to SQL, or storing to some kind of document (txt, xml, csv) in your server (('user Mike clicked date link at this time/day')), and then sending that detail down to your javascript when you load the page.
Store it in the URL as a query or hash parameter. This can be changed or erased by the user so it cannot be relied upon to be there (or not be there) when you expect it.
Any of those would be the correct method to achieve the goal, and each one is well documented everywhere.
Cheers
I'm trying to get a cookie I have set but all I'm getting is the PHPSESSID.
I set my cookie in a separate PHP page with:
setcookie("username", $sentname, time()+(60*60*24*30),NULL,NULL,NULL,false);
I can then find it through firefox settings.
When I try to access it using JavaScript on a different page I use:
<script>
$(document).ready(function(){
var cookie = document.cookie;
alert(cookie);
});
</script>
which then returns:
PHPSESSID=gvjsgfd8etlbdq43lndni3o0g4
It should return all the cookies, only "username" so far, I have set in a key paired string. I tried using the jquery plugin for cookies and it returned the same thing. I also couldn't find this problem elsewhere online.
Not sure if I should delete the question but one of the related links gave me the answer.
The cookie path must be set to '/' to be accessible from all subdomains. so I changed it too:
setcookie("username", $sentname, time()+(60*60*24*30),'/',NULL,NULL,false);
which gives me:
PHPSESSID=gvjsgfd8etlbdq43lndni3o0g4; username=asdf
I am working on a form and when you focus on an input field, I have a pop up that reminds them to save when they are complete. I only want the user to see this pop up once every 60 days. On focus I set a cookie that tells me they have seen the alert. Then however, since I only want this to appear every 60 days, I want to make sure it doesn't happen again next time they click. How do I execute something like, "if this cookie exists, then do this?" Below is a sample of the code I'm using:
$("input").focus(function(){
//My alert goes here (i'm good on this part")
Cookies.set('saveChanges', 'wasAlerted', { expires: 60, path: '/' });
//if SOMETHING, do SOMETHING ("need help here")
});
FYI, My cookie name is 'saveChanges' and the value is 'wasAlerted'.
https://github.com/js-cookie/js-cookie
Seeing as the value of the cookie is redundant, you can just check if there's a cookie matching saveChanges.
$("input").focus(function(){
if (Cookies.get('saveChanges') == null) {
//Alert
Cookies.set('saveChanges', 'wasAlerted', { expires: 60, path: '/' });
}
}
Side note, I'd recommend using the jQuery Cookie Plugin for organization's sake. The API is not too much different but it will make your code more consistent.
I'm not sure if the way to do this is check Google Analytics cookies or otherwise track where a user came to my site from. Basically I have a form with a hidden field code="XY1" Now I need to be able to insert a different preset code for say people who came from Facebook, so the script would have to check where the visitor came from and then assign a code XF1 to any from FB, and a code XT1 to any from Twitter, etc.
Would something like this PHP work for the capture?:
$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( stristr( $referringPage['host'], 'facebook.com' ) )
Or this JS
var ref = document.referrer;
if (!ref.indexOf("facebook.com") != -1) {
document.write(...)
}
I'm not sure what is the best way to do it and what kind of methods can reliably check the source of a visitor, so any help would be greatly appreciated.
You can use $_SERVER['HTTP_REFERER'], but it's not guaranteed to be accurate, or even present. Not all browsers will necessarily set it, and some allow you to set it yourself. Google cookies won't contain any site history, and you can't examine the browser history, so there's no guaranteed way to do what you're asking.
You can try this option using jquery $.test() method.
$(function(){
var referer=document.referrer, //option 1
//referer="<?php echo $_SERVER['HTTP_REFERER'];?>",//optional 2
XFB=/facebook.com/g,
XFT=/twitter.com/g,
checkF1=XFB.test(referer),
checkF2=XFT.test(referer);
if(checkF1){
var code= "XF1";
$('#hiddenInput').attr('value','ref: '+referer)
}
else if(checkF2){
var code= "XT1";
$('#hiddenInput').attr('value','ref: '+referer)
}
});