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");
});
Related
I want to set cookie value in one function and their value use everywhere in cakephp 4 version.
Currently , i can use cookie value inside the only one function which i have set their value.
I can get cookie value in index() function but i can't get cookie value in viewusers() function.
Code is here :
use App\Controller\AppController;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection
use DateTime;
class AdminController extends AppController {
function index(){
$cookie = array();
$cookie['admin_username'] = $requestData['username'];
$cookie['admin_password'] = $requestData['password'];
$cookies = new Cookie('AuthAdmin',$cookie, new DateTime('+1 weeks'));
$response = $this->response->withCookie($cookie);
return $this->redirect('admin/viewusers');
}
function viewusers() {
$cookies = new CookieCollection();
$data = $cookies->get('AuthAdmin');
print_r($data);
// cookie value not found in $data variable.
$response = $this->response->getCookie('Auth.Admin');
print_r($response);
// cookie value not found in $response variable.
}
}
I can get cookie value in index() function but i can't get cookie value in viewusers() function.
Try this:
function index(){
$cookie = array();
$cookie['admin_username'] = $requestData['username'];
$cookie['admin_password'] = $requestData['password'];
$cookies = new Cookie('AuthAdmin',$cookie, new DateTime('+1 weeks'));
return $this
// redirect returns a response object, so you can chain the cookie call onto that
->redirect('admin/viewusers')
// Note that this uses $cookies, not $cookie
->withCookie($cookies);
}
But again, I cannot stress strongly enough that sending a cookie with the user's username and password in it is a very bad thing from a security perspective.
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.
I am trying to set an array as an environmental variable in postman.
But it stores the first value of the array rather than the array.
var aDataEntry = postman.pm.environment.get('data_set_entries');
if(aDataEntry == null) {
aDataEntry = [];
}
var jsonData = pm.response.json();
aDataEntry.push(jsonData.dataEntry.id);
// a console.log here confirms that aDataEntry is an array
postman.pm.environment.set('data_entry',aDataEntry);
As mentioned in the comment of the code, the variable is coming as an array,
but when I again get the environment variable in the second run, it is not
of type array. But just contains the first element in the array.
What's wrong here?
How can set the array and use it from the postman environment variable.
It seems like pm.environment.set calls toString to set an environment value. You can use the below code to work-around that:
var aDataEntry = pm.environment.get('data_set_entries');
if(aDataEntry == null) {
aDataEntry = [];
} else {
aDataEntry = JSON.parse(aDataEntry);
}
var jsonData = pm.response.json();
aDataEntry.push(jsonData.dataEntry.id);
// a console.log here confirms that aDataEntry is an array
pm.environment.set('data_entry',JSON.stringify(aDataEntry));
Edit 1:
As mentioned in the Postman reference docs, it is suggested that one use JSON.stringify() and JSON.parse() for storing complex objects. I have updated the code accordingly.
I'm not sure of how you intend to use the array, but to dynamically generate an array for use in a Body > raw > JSON POST, as in the answer above you do need to actually store the var as a string.
Here's an example of that and it's use in the POST Body. I had a long list of IDs, and I'm using Postman to do some bulk user profile updates.
In the Pre-request Script, generate the string to be POSTed as an array.
var externalIds = [111,222,333,444];
var attrString = "";
externalIds.forEach(userId => {
attrString += `,{"external_id": ${userId},"my_first_attribute": false,"my_next_attribute": true}`;
});
attrString = attrString.replace(',',''); // strip out that 1st unwanted comma
pm.environment.set("attributeArray",attrString);
The saved "array", Postman console logged:
"{"external_id": 111,"my_first_attribute": false,"my_next_attribute": true},
{"external_id": 222,"my_first_attribute": false,"my_next_attribute": true},
{"external_id": 333,"my_first_attribute": false,"my_next_attribute": true},
{"external_id": 444,"my_first_attribute": false,"my_next_attribute": true}"
Looks like bad, nested double quotes, but the format is actually valid.
My Body > raw looks like:
{
"api_key": "{{api_key}}",
"attributes": [{{attributeArray}}]
}
Note the Postman variable is wrapped in "[" and "]".
If my externalIds array needed to be a pm variable, I'd store that as a string, and .split() it when using it in the Script tab.
The Postman console really helps get past the syntax mistakes.
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 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.