Cookie set in codebehind not set on server but works local - javascript

To check if a document is sent to the browser I'm creating a cookie and checking if it exists in javascript. I got it to work on localhost, but for some reason it doesn't work after I deployed it to the server.
Codebehind:
HttpCookie cookie = new HttpCookie("download", "complete");
cookie.Expires = DateTime.Now.AddSeconds(10);
Response.Cookies.Add(cookie);
Javascript:
function waitForDownload() {
if (getCookie("download") === "complete") { //getCookie method works
//do something
} else {
setTimeout(waitForDownload, 500);
}
}
I think the cookie doesn't get created, I can't find it when looking for it in the chrome console (Checking before the 10 seconds are over).

I believe you may need to add this to allow the cookie to be accessed by javascript. Try:
cookie.HttpOnly = false;

Related

Checking for file existence in a local domain

I am pulling my hair out trying to figure this out since there are numerous examples of how to do this and none seem to work for me the way they are suppose to. I want to check for the existence of a local file. I have placed a file in the same directory as the html code and it is accessible and readable. This is NOT a URL or permission problem. Here is one example of code I am trying to use. checkURL() is called with the URL in the global variable linkURL.
function doesFileExist(urlToFile)
{
var xhr = new XMLHttpRequest();
xhr.open('HEAD', urlToFile, false);
xhr.send();
if (xhr.status != "200") {
return false;
} else {
return true;
}
}
function checkURL()
{
var result = doesFileExist(linkURL);
if (result == true) {
alert("Exists");
// Page would be loaded here with window.location.href = linkURL;
} else {
alert("NOT Exists");
}
}
The problem is this will always return true but never returns false and never alerts "NOT exist" Somehow it just bypasses that alert code. I have tried many variations of this and in all cases when the URL does not exist the test fails to display anything but always works when it exists. My goal here is to access multiple local URL's and display those that exist but if one does not exist I want to be able to display a message to that effect to the user and not actually access the URL and get a page error. The files and HTML code will ultimately be on a DVD and accessed by a browser.

passing data from authentication cookie to javascript

I am using an authentication cookie passed between websites on the same domain. The cookie contains some user info and page info (the accession number). The design goal is for the user to click a button on the referring website, and it will launch a second website, authenticate based on the cookie, and do some useful stuff with the accession number. I got most of this built, including getting the authentication passed and properly parsed out on the receiving system.
The problem I am having is that I can't get the data within the cookie into the javascript on the page. It seems when i launch website2 from website1, $(document).ready() is not fired after the page_load event (which handles the cookie parsing). Also I tried using a literal to post the javascript code, it's never fired (seemingly it places it after the client side stuff is executed.
What I really want to do is call a javascript function getResults(accnum) using this data.
I have this code on the page_load event:
if (userdata != null)
{
accnum = userdata[4];
}
if (accnum != String.Empty)
{
//HttpCookie accnumcookie = new HttpCookie("accnum", accnum);
//this.Context.Response.Cookies.Set(accnumcookie);
}
}
When I run the .Set function, I'm not really sure of the innards and details, but long story short, the cookie is set but does nothing.
This is the document.ready.
$(document).ready(function () {
var accnum = new String();
accnum = GetCookie('accnum');
if (accnum != null) {
document.cookie = 'test=testz';
var srch = document.getElementById('crit');
srch.style.display = 'none';
getResults('', 'accnum', accnum);
}

Repeating/looping javascript function to check if a cookie has changed

I am trying to make a script that will repeat every 5 seconds.
The script that will be repeated will check to see if a cookie exists.
If the cookie does not exist, the page is redirected.
If the cookie does exist, nothing happens.
The cookies are working fine, my only problem is that it doesn't repeat!
I am using jQuery to identify/check the cookie and that is working fine.
I would like to know what is wrong with the code please.
I have looked online many times, but have had no luck in finding what I need.
This is the cookie plugin I use: https://github.com/carhartl/jquery-cookie
var checkcookie = $.cookie('myCookie');
checklogin();
function checklogin(){
setTimeout(function(){
if(checkcookie == null){
//if cookie not set
window.location.href='/';
}
else{
//if cookie set
}
}, 5000);
checklogin();//to recall the script after it is done
}
Or if any one has an alternative method of checking whether a cookie has changed, I would love to know!
It's working fine, but your cookie value not changing - it set once and forever. Try to include it in your setTimeout():
checklogin();
function checklogin(){
setTimeout(function(){
var checkcookie = $.cookie('myCookie');
if(checkcookie == null){
window.location.href='/';
}
else{
//if cookie set
}
}, 5000);
checklogin(); //to recall the script after it is done
}
If you're doing it that way the checklogin call has to be inside the setTimeout function otherwise it's called immediately everytime.
Also i'd use a setInterval instead, so you don't need you recursive function:
setInterval(function(){
if(checkcookie == null){
window.location.href='/';
}else{
//if cookie set
}
},5000);

Is Local Storage Persistant?

I'm using Local Storage for a Chrome extensions. When I set the local storage using
localStorage['tracking'] = false;
... and then check to is local storage set using:
if(localStorage['tracking'])
{
alert('set');
} else {
alert('Not Set');
localStorage['tracking'] = true;
}
It works fine. It sets tracking to true if unset and remains set. But if I go to a new tab, and to the same page I was on in the other tab, it returns unset.
Is there any way for local storage to remain persistant, even after the browser has closed?
Edit: I am not setting the localStorage array element at the top if the script. Just realised that might confuse readers.
Try:
localStorage.setItem('tracking', false);
and
localStorage.getItem('tracking');
These are the methods used in the W3C API Specification.
Since the usage you mentioned is not specified, depending on browser implementation it is possible that you are adding new properties to the localStorage global variable in the current window object, which become unavailable when navigating away from the page.
LocalStorage works only with strings. Check this explanation.
I used to have the same problem. I would like to share my solution. However, my case may not be the same as your case.
My case ist that I had this code (localStrage['xxxx']='xxx';) in a content script. Then I had the problem when open the new tab or open new windows.
So, my solution is that I did not run this code by the content script directly. The content script will call 'chrome.extension.sendRequest' and the background.html will do set/read localStrage and return to the content script.
This will be the code in content script
chrome.extension.sendRequest({action: 'load'}, function (response) {
if(response){
alert('set');
} else {
alert('Not Set');
chrome.extension.sendRequest({action: 'set',value:'true'}, function (response) {});
}
}
This will be the code in background.html.
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.action == 'load') {
sendResponse(localStorage['tracking']);
}else if (request.action == 'set') {
localStorage['tracking']=request.value;
}
});

HTML5 Storage Problem

I have a notes box that can be used to store notes, originally I was using cookies to store the entries, then I tried HTML5 Storage and I can't get it to work, here is the code:
$(document).ready(function () {
$('#savesNotes').click(function () {
localStorage.nltwonotes=document.forms[0].todo1.value;
}
});
document.forms[0].todo1.value=localStorage.nltwonotes;
});
Here is some working code:
Preview (type in, then view your localStorage in your console) :
http://jsbin.com/exote5/
Source:
http://jsbin.com/exote5/edit
$(document).ready(function () {
$('#saveNotes').click(function () {
localStorage.nltwonotes=$('#note').val();
});
if(localStorage.nltwonotes){
$('#note').val(localStorage.nltwonotes);
}
else{
//Not set yet
}
});
== NOTE ABOUT THE FILE:/// PROTOCOL ==
You must have localStorage on a server (http or https). Firefox will not let you use them locally. If you are using a Mac you can use MAMP or on Windows you can use WAMP If you're on Linux you probably already know how to setup a local hosting enviroment with apache which is usually provided...
I'd recommend using the methods provided to access localStorage instead of how you're doing it:
localStorage.setItem("nltwonotes", document.forms[0].todo1.value);
Then to get a value:
document.forms[0].todo1.value = localStorage.getItem("nltwonotes");
Though, make sure you check to see if localStorage is even available:
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
Otherwise you'll want to fallback / tell the user to upgrade their browser to one that supports local storage.

Categories

Resources