Javascript PopUp once per day - javascript

I have javascript popup it shows up per user (only once per day) and i need same popup but second.. when i try just to create second popup with same code it works like first one. if user saw first popup, he/she can't see other. please help here is the code
if (document.cookie.indexOf('_visited=1') == -1) {
var delay_popup = 1000;
setTimeout("document.getElementById('parent_popup').style.display='block'", delay_popup);
var date = new Date;
date.setDate( date.getDate() + 1 ); // Current date + 1 day
document.cookie = '_visited=1; path=/; expires=' + date.toUTCString();
}
I don't want them to work like 1 Popup

I just had this problem today.
what i did is this
enter code here
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
}
var myCookie = readCookie('daycookie');
if (myCookie=''){
// code to show modal here
// then set an expiring token at midnight
var date = new Date();
var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59);
var x = Math.floor((Math.random() * 1000) + 1);
document.cookie = 'daycookie' + "=" + x + "; " + "expires="+midnight;
}
the logic is at first if the cookie does not exist show the popup
once the pop up shows it creates a cookie the expires at midnight and until next the day will the pop up show again.
i hope this will help you.

Related

Javascript Calendar needs new function

First of all, this forum has been outstanding at helping me learn the limited scripting I do understand so thanks to everyone who contributes.
I have been using the following script on my website to generate a dropdown menu of months/years for a form:
<script>
function CreateMonthDropDown(){
var arrMonthName = new Array();
arrMonthName =
["January","February","March","April","May","June","July","August","September","October","November","December"];
var ThisDate = new Date();
var ThisMonth = ThisDate.getMonth();
var ThisYear = ThisDate.getFullYear();
var tmpMonthYear;
var tmpMonthYearName;
document.write(" <option value=\"\">Any Date</option>");
for ( i = 0; i < 36; i++) {
if ( ThisMonth == 12 ) {
ThisMonth = ThisMonth - 12;
ThisYear = ThisYear + 1;
}
if ( (ThisMonth + 1) < 10 ) {
tmpMonthYear = '0' + (ThisMonth + 1) + '*' + ThisYear + '*';
} else {
tmpMonthYear = (ThisMonth + 1) + '*' + ThisYear + '*';
}
tmpMonthYearName = arrMonthName[ThisMonth] + ' ' + ThisYear;
document.write(" <option value=\"" + tmpMonthYear + "\">" + tmpMonthYearName + "</option>");
ThisMonth = ThisMonth + 1;
}
}
</script>
<font size="1">Travel Date:</font><br><select name="srchStartTravDate">
<script>CreateMonthDropDown();</script></select>
This is great for generating month/year and removing outdated options. However, an API search I use has recently changed their values. Here's what I need:
I need the display to remain the same to the user - January 2018, February 2018 etc.
But I need the value passed through on the form in the following format:
Month needs to be reflected in a two digit number 01, 02 etc. with a * on the end
Year needs to be reflected in a four digit number 2018, 2019 etc. with a * at end
All entries need to be appended with &DateRangeType=4
So for example, May 2018 would pass the value of: 05*2018*&DateRangeType=4
I've searched and searched for a script that will do this and can't find it or figure out how to adapt it to make this work. Is it possible? Any advice would be greatly appreciate. Thanks in advance.

AJAX and setTimeout on Internet Explorer

I'm trying to get the server datetime using ajax. I'm having a trouble running the script on Internet Explorer.
My old code was like this. (But it only displays client's pc datetime which can be changed anytime by the client)
var _current = new Date();
var _day = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][_current.getDay()];
var _month = ["January","February","March","April","May","June","July","August","September","October","November","December"][_current.getMonth()]
var _date = _current.getDate(); if(_date < 10){_date = "0" + _date;}
var _year = _current.getFullYear();
var _hours = _current.getHours(); if(_hours > 12){_hours = _hours - 12; var ampm = "PM"}else{var ampm = "AM"}if(_hours < 10){_hours = "0" + _hours;}
var _minutes = _current.getMinutes(); if(_minutes < 10){_minutes = "0" + _minutes;}
var _seconds = _current.getSeconds(); if(_seconds < 10){_seconds = "0" + _seconds;}
$("#datetime").html("");
$("#datetime").html(_day + ", " + _month + " " + _date + ", " + _year + ", " + _hours + ":" + _minutes + ":" + _seconds + " " + ampm + "");
output: Wednesday, December 02, 2015, 11:47 AM (GMT+8)
So, I switched to AJAX and made a PHP page.
function getDateTime(){
$.ajax({
url: 'datetime.php',
success:function(content){
$("#datetime").html("");
$("#datetime").append(content);
}
});
window.setTimeout(getDateTime,1000);
}
php
<?php
// Set Timezone
date_default_timezone_set('Asia/Taipei');
// Display DateTime
echo date("l, F d, Y, h:i:s A",strtotime('Now'))."(GMT".date("O",strtotime('Now')).")";
?>
output: Wednesday, December 02, 2015, 11:47 AM (GMT+0800)
To me it looks like the issue with caching of the ajax call because the request URL will be same all the time. You can try
$.ajaxSetup({
cache: false
});
This will add some random request query string and will break any caching issues, if any. May be worth a try.

Detecting DataTimeZone

I would like to automatically detect visitors timezone so the date and time displayed reflects their current time. I realize this isn't possible with PHP alone but will need some JavaScript as well.
What can I use to replace the value of $timezone in the code below to automatically detect a visitors timezone?
<?php
// Date & time format.
$date_format = "m/d/Y";
$time_format = "h:i A";
$timezone = "America/New_York";
// Get current datetime.
$date = new DateTime();
// Set timezone.
$date->setTimezone(new DateTimeZone($timezone));
// Echo current date and time.
echo $date->format($date_format . " " . $time_format);
?>
Returns:
07/22/2014 03:03 PM
Using java script you can use it
<script src='http://code.jquery.com/jquery-2.1.1.js'></script>
<script src="https://bitbucket.org/pellepim/jstimezonedetect/raw/f9e3e30e1e1f53dd27cd0f73eb51a7e7caf7b378/jstz.min.js"></script>
<script>
$(document).ready(function(){
var timezone = jstz.determine();
$("#div").html(timezone.name());
});
</script>
<div id='div'></div>
Remember that download jstz.min.js it doesn't allow embed from https://bitbucket.org/
if you want see it working giv one look at
http://gomusic.designerbh.com/teste.php
It would be best to use Javascript
See this fiddle for an example
jQuery(document).ready(function() {
var foo = jQuery('#foo');
function updateTime() {
var now = new Date(),
d = [];
d[0] = now.getFullYear().toString(),
d[1] = now.getMonth()+1, //months are 0-based
d[2] = now.getDate(),
d[3] = now.getHours(),
d[4] = now.getMinutes();
//doing YY manually as getYear() is deprecated
//remove the next line if you want YYYY instead of YY
d[0] = d[0].substring(d[0].length-2); //not using substr(-2) as it doesn't work in IE
//leading zeroes
for (var i=1; i<=4; i++)
if (d[i] < 10) d[i] = '0' + d[i];
foo.val(d[0] + '-' + d[1] + '-' + d[2] + ' ' + d[3] + ':' + d[4]);
}
updateTime();
setInterval(updateTime, 5000); // 5 * 1000 miliseconds
});

Save excel file with current date and time through javascript

I want to open the excel file and then save the excel file and the name will be file name + current date and time. I am not getting this result for date I have used Date()
var wshell;
var excel = new ActiveXObject("Excel.Application");
alert(excel);
var excel_file = excel.Workbooks.Open("book2.xlsx");
excel.Visible = true;
var objWorkbook = excel.Workbooks.Add();
var objWorksheet = objWorkbook.Worksheets(1);
objWorkbook.Worksheets(1).Activate;
objWorksheet.name = "test";
objWorksheet.Paste;
objWorksheet.columns.autofit;
window.clipboardData.setData("Text","");
var today = new Date();
document.write(today.toString());
excel_file.SaveAs("d:\\board.xls"+ (today.toString()));
alert("data saved");
today contains an illegal character (:) to use in a file name. You need to clean your date, for example something like this:
var today = new Date().toString().replace(/[^\w]/g, '');
And when saving, the timestamp should be a part of the file name instead of the extension:
excel_file.SaveAs("D:\\board" + today + ".xls");
Instead of .toString().replace() you can format the timestamp to look like you want with methods of Date object.
EDIT
Here's the code with which you can modify your dates. I've simplified getDate() for you, hence you can modify it to return a date in what ever form you want.
var today = new Date(),
time = today.toTimeString().split(':').join('').substr(0, 4),
timestamp = getDate('dd_mm_yyyy', today) + '_' + time;
function getDate (mode, userdate) {
var dte = userdate || new Date(),
d = dte.getDate().toString(),
m = (dte.getMonth() + 1).toString(),
yyyy = dte.getFullYear().toString(),
dd = (d.length < 2) ? '0' + d : d,
mm = (m.length < 2) ? '0' + m : m,
yy = yyyy.substring(2, 4);
switch (mode) {
case 'dd_mm_yyyy': return dd + '_' + mm + '_' + yyyy;
case 'yyyymmdd': return yyyy + mm + dd;
default: return dte;
}
}
timestamp contains a date in wanted form after run the code above.

How can I delete all cookies with JavaScript? [duplicate]

This question already has answers here:
Clearing all cookies with JavaScript
(26 answers)
Closed 6 years ago.
I have written code to save cookies in JavaScript. Now I need to clear the cookies irrespective of values that I assigned.
Are there any script modules to delete all cookies that were generated by Javascript?
My Sample Code:
document.cookie = 'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
How else could I clear all of the cookies?
Will there will be any problems when I test the code on the webserver?
There is no 100% solution to delete browser cookies.
The problem is that cookies are uniquely identified by not just by their key "name" but also their "domain" and "path".
Without knowing the "domain" and "path" of a cookie, you cannot reliably delete it. This information is not available through JavaScript's document.cookie. It's not available through the HTTP Cookie header either!
However, if you know the name, path and domain of a cookie, then you can clear it by setting an empty cookie with an expiry date in the past, for example:
function clearCookie(name, domain, path){
var domain = domain || document.domain;
var path = path || "/";
document.cookie = name + "=; expires=" + +new Date + "; domain=" + domain + "; path=" + path;
};
On the face of it, it looks okay - if you call eraseCookie() on each cookie that is read from document.cookie, then all of your cookies will be gone.
Try this:
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
eraseCookie(cookies[i].split("=")[0]);
All of this with the following caveat:
JavaScript cannot remove cookies that have the HttpOnly flag set.
This is a function we are using in our application and it is working fine.
delete cookie: No argument method
function clearListCookies()
{
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
var spcook = cookies[i].split("=");
deleteCookie(spcook[0]);
}
function deleteCookie(cookiename)
{
var d = new Date();
d.setDate(d.getDate() - 1);
var expires = ";expires="+d;
var name=cookiename;
//alert(name);
var value="";
document.cookie = name + "=" + value + expires + "; path=/acc/html";
}
window.location = ""; // TO REFRESH THE PAGE
}
Edit: This will delete the cookie by setting it to yesterday's date.
Why do you use new Date instead of a static UTC string?
function clearListCookies(){
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++){
var spcook = cookies[i].split("=");
document.cookie = spcook[0] + "=;expires=Thu, 21 Sep 1979 00:00:01 UTC;";
}
}

Categories

Resources