I have implemented intro.js to my website. But I wanted to start the tour only on first visit. may be by using cookies.. website is made with html no php..
JavaScript cookies are a solution although I should point out that it will only work for as long as the user keeps the cookie.
//set the cookie when they first hit the site
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
//check for the cookie when user first arrives, if cookie doesn't exist call the intro.
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
code is from http://www.w3schools.com/js/js_cookies.asp
obviously there's some blanks you'll have to fill in there, but it's a good starting point for working with cookies in javascript.
EDIT:
So you want to make a new function, put it in the head, inside script tags (if you have them already, just copy the function into there (you'll want to put the other two functions I provided within the script tag also)). This function will check to see if you have a cookie. If you do, just return. If you don't, create the cookie and run the intro,
<head>
<script type="text/javascript">
function checkCookieIntro(){
var cookie=getCookie("mySite");
if (cookie==null || cookie=="") {
setCookie("mySite", "1",90);
runIntro(); //change this to whatever function you need to call to run the intro
}
}
</script>
</head>
now change your body to be:
<body onload="checkCookieIntro()">
so when the body loads it will check to see if a cookie exists, if it doesn't, create a cookie with a value of one that will last for 90 days (unless the user deletes it) and then run the intro. If the cookie does exist with a value then it does nothing.
Related
I am trying to write my user ID to cookie. I can catch it from dataLayer variable from server when user is login in but user can be already logged in next session. So I tried to create custom JavaScript code that suppose to check if variable is not undefined and write a cookie and if the cookie exist then returns cookie value.
function() {
if ({{UID dataLayer}}) {
var d = new Date();
d.setTime(d.getTime() + 1000 * 60 * 60 * 24 * 365 * 2);
var expiresdate = 'Expires=' + d.toGMTString();
document.cookie = 'UIDcookie=' + {{UID dataLayer}} + '; ' + expiresdate + '; Path=/';
return {{UID dataLayer}};
} else if ({{UID Cookie}}) {
return {{UID Cookie}};
}
return;
}
But it works only when I'm in tagmanager debug mode, when it's off nothing suppose to happen. So my question is what am I doing wrong?
It's probably a question of timing. Try to fire the tag to the page load, in this way the function in JS variable should be able to read the cookie.
function sendText(id,text) {
if(text == "hiii"){
var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + "sup?";
} else{
var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + "yo yo yo";
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
}
}
My issue is that in Google Scripts (back end to Google Sheets), I have this function that reads in a message from telegram and, if the message reads "hiii" it should respond "sup?". Currently my code does not do this and instead executes only the else statement.
I have the following search criteria working very well in Gmail:
user#domain from:/mail delivery/ || /postmaster/ ||/Undeliverable/
I am trying to write Goole Apps code to return the same results. Here is the code:
var thread=GmailApp.search("user#domain from:/mail delivery/ || /postmaster/ ||/Undeliverable/ ");
I am getting different results. I am new to both Regex and Google Apps.
Try Amit Agarwal's tutorial on Gmail Search with Google Apps Script which includes Using Regular Expressions to Find Anything in your Gmail Mailbox:
function Search() {
var sheet = SpreadsheetApp.getActiveSheet();
var row = 2;
// Clear existing search results
sheet.getRange(2, 1, sheet.getMaxRows() - 1, 4).clearContent();
// Which Gmail Label should be searched?
var label = sheet.getRange("F3").getValue();
// Get the Regular Expression Search Pattern
var pattern = sheet.getRange("F4").getValue();
// Retrieve all threads of the specified label
var threads = GmailApp.search("in:" + label);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var msg = messages[m].getBody();
// Does the message content match the search pattern?
if (msg.search(pattern) !== -1) {
// Format and print the date of the matching message
sheet.getRange(row,1).setValue(
Utilities.formatDate(messages[m].getDate(),"GMT","yyyy-MM-dd"));
// Print the sender's name and email address
sheet.getRange(row,2).setValue(messages[m].getFrom());
// Print the message subject
sheet.getRange(row,3).setValue(messages[m].getSubject());
// Print the unique URL of the Gmail message
var id = "https://mail.google.com/mail/u/0/#all/"
+ messages[m].getId();
sheet.getRange(row,4).setFormula(
'=hyperlink("' + id + '", "View")');
// Move to the next row
row++;
}
}
}
}
I want to make a auto login when users used APP after first time.I tried to keep it in alloy.js and Ti.App.Properties.getString('login_token'),but they didn't work.
in my coffee:
result = JSON.parse this.responseText
console.info result.token #"dsfdsfds2142fds3r32rf32e3dfefwedf"
Ti.App.Properties.setString "token",result.token
console.info Ti.App.Properties.getString "token" # it's blank
I couldn't find a built in way to do this so i just created a getter and setter method and put it in alloy.js. Feels very hacky and dirty but it does work.
//retrieves the value of the token
// also checks if the token is valid and sets logged_in accordingly
function getToken(){
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'api.txt');
var content = f.read();
//if we can read this return it, otherwise return a blank value
if (content){
return content.text;
}
else {
return '';
}
}
//persists and sets the value of token to the new value
function setToken(key){
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'api.txt');
f.deleteFile();
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'api.txt');
f.write(key);
token = key;
}
I have a text field and a submit button. User submits an email id and on successful submission a lightbox will load. On successful submission I am setting a cookie and initially assigning a value of 1. Though I am able to set the increment I am not able to assign the incremented value to the cookie value.
var demoCookieCount = getCookie("democount");
$('#online-demo-lightbox-button').click(function (e) {
e.preventDefault();
demoCookiecount++;
});
You have to store the new value in the cookie by using setCookie() or something like
function setCookie(democount, ++demoCookiecount, 1)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
You don't save the new value into the cookie. Try something like that in the end:
document.cookie = cookieName + cookieContent + cookieExpires;
Finally somehow got through. This piece of code worked
//setting the Cookie democount with initial value of 1
setCookie("democount", '1');
//getting the value of 'democount' cookie and assigning it to a variable
var demoCookieCount = getCookie("democount");
$('#sample-button').click(function (e) {
e.preventDefault();
//increment the variable on every click
demoCookieCount++;
//set the incremented value of demoCookieCount variable to democount cookie
setCookie("democount",demoCookieCount);
// assigning the value of demoCookiecount to variable 123
var123 = getCookie("democount");
});