Check localStorage is set to a specific value? - javascript

I'd really appreciate some help on this, I really can't see my issue here.
For some reason, sensing whether this data (loggedIn) is stored in localStorage with the value "yes" - I've tried the regular way of seeing if the localStorage is empty.
Here's my current code...
<script>
function checkLoggedIn()
{
if (localStorage.getItem("loggedIn") === null) {
alert("empty");
}else{
$(".topbar").html("<span>Saved Jobs(0)</span><span>Welcome, "+ storedUsername +"!</span>");
alert("working");
}
}
</script>
Then obviously I would call this with a body onload. For some reason, I can physically see the value of the localStorage when passing it as a variable, but checking it seems to not be working in the slightest.
I'm using this to simulate functionality of a user logging in, the rest of the functions to handle registration and logging in work fine to actually set the localStorage - so it's not a case of it not being stored.
Thanks for any help given people, it's really appreciated.

Related

Appmaker: How can I create and access global variables?

I am trying to make a button which acts as a switch, enabling the visibility of a panel.
I am running a client side script when the onClick event fires, which is the following:
function {
if(app.datasources.global.item.hideshow===false)
{
*does one thing*
}
else if(app.datasources.global.item.hideshow===true)
{
*does another*
}
}
My problem is, that the global (which is the datasource).item seems to be a null according to the console error log. It seems like i am trying to access one property of a record from a database, but I would like to access and edit a property which is not attached to any database, it would be just a "global variable".
Maybe I haven't phrased it too well, but I hope somebody can help me out with this. Thank you in advance.
There are a few ways you could do this. This link may help ontoggle Event.
Another way I could see doing this would be using local storage.
You can use a custom property for that. So in the page where you want to toggle the panel, create a custom property and perhaps call it panelVisibility. Then you can use the following logic on the onclick event handler of the button:
var visible = widget.root.properties.panelVisibility || false;
if(visible){
//do someting
} else {
//do other thing
}
widget.root.properties.panelVisiblity = !visible;

window.location.state.buttonEnabled always fails check for undefined with ReactJS

I'm trying to read the value of window.location.state.endButtonEnabled to check for undefined. But when I actually go ahead and check for undefined I get an undefined error at the if condition. In the following code:
constructor(props){
super(props);
if(window.location.state.endButtonEnabled !== undefined){
console.log('endButtonEnabled is: '+ window.location.state.endButtonEnabled );
//Do Something
}
else{
window.location.state = {endButtonEnabled: true};
}
}
Consider the error in the console:
I tried similar variations of this SO answer to no avail. Any suggestions? I'm using the latest version of ReactJS.
There is no "state" property in the window.location object. So window.location.state is undefined the first time you run the check, breaking your code. It doesn't even get to the endButtonEnabled property.
Why are you putting state into window.location anyways? I assume you are trying to manage the state of your component? You shouldn't be doing that inside of window.location, the entire window object is made by the browser and not something you should be altering without good reason. Look at these react docs to better understand how to manage state.
I decided to first check for window.location.state === undefined. This lead to the following solution while using navigate('/path/destination'). Surprisingly navigate() from '#reach/router' preserves window.location.state so I was able to use that when coming back from the next page. So the final code I ended up with for the check is:
if(window.location.state === undefined){
window.location.state = {endButtonEnabled: false}
}
else{
window.location.state = {endButtonEnabled: true}
}
This code does not break when loading the page for the first time. This may not be the ideal way to state manage things, but this way works for me. I guess the takeaway is: do as the docs say, not as I do.

AfterSave on PFUser checking if on creation or update

I am having difficulty in distinguishing between if a PFUser is being updated or if it is being saved for the first time. I implemented the solution found on parse.com here. However, it is not working as explained in the answer found at the link. I always get a false regardless of if it is on the creation or an update. Here is the code that I have implemented.
Parse.Cloud.afterSave(Parse.User, function(request) {
Parse.Cloud.useMasterKey();
//this error log prints false regardless of if the user is signing up.
// or having a key updated.
console.error(request.object.existed());
if (!request.object.existed()) {
//This is on sign up, the user has not existed prior.
//This doesn't work. This block of code is always run.
}
else {
//This is supposedly when the user is having a key updated.
//Unfortunately, this code is never run regardless.
}
)}
This code does not behave as I expect it to because the request.object.existed() always returns false. I think this may have to do that this is saving a PFUser and not some generic PFObject. Is there something I am missing using request.object.existed() on a PFUser afterSave? Thanks.
This seems to be an unresolved Parse cloud bug
Possible workaround is to check the difference between create and update times to see if your object is being updated or is being created.

Doing things on a new page

Quick question about going to a new page and doing stuff.
// doesn't work
function doThings() {
document.location.assign("http://foo.com");
things.doIt();
}
foo.com has a things.doIt() function that I want to invoke after switching to that page, but it appears I can't really do that. Is there an easy way to do this?
And side question, I see everyone using document.location = url even though location is an object; it appears to work fine but why does everyone use it? Is it faster?
Edit: I can't edit anything on foo.com.
What you could do is pass a "referer" or action key in the URL query string so that foo.com can tell if it was arrived at from a specific page or should do something special. Note: someone could just manually add it to the URL as well though.
http://foo.com?referer=bar.com
or
http://foo.com?action=doIt
Anyway, what you would want is to have a window.onload event handler on foo.com, and inside it have a function to run onload and check if there exists a key in the query string that matches what would be set from the referer, and if so, run things.doIt()
window.onload = function(){
//check the value of location.search for your query string
//execute special code if so
}

jQuery unload(): Syntax error after updating a Cookie - how to find the cause?

Solution below: Edit #2
I've a HTML-list the user is able to sort. I don't want to save the data after every drag/drop action, so I save the data on unload: in a cookie and database. Thats working, but:
After saving the data the list is hidden and I get a "syntax error" in this line:
<!DOCTYPE html>
It's strange because everything works fine after refreshing the same page (F5) without changing anything.
I try to find the cause but no success. That's the flow:
1. visit the page (index.php)
2. change the list (set: list_is_dirty = true)
3. click any internal link (call $(window).unload( ... save_data() ... )
4. target page appears without the list (syntax error!)
5. refreshing the page (everything works fine)
Do you have any idea how to find this error? Any tools or strategies? Or maybe the same experience with the unload function?
Thanks in advance!
Edit:
Some code:
var list_is_dirty = false;
// document ready?
$(function() {
function sort_list() {
// some code, not important
}
sort_list();
$(window).unload(function() {
if (list_is_dirty == true) {
/* ---------- HERE's the error! ---------- */
/* The error occures when I try to call the script.php
I tried load(), $.post(), $.get() but nothing works.
The string is correct. I'm not even able to call any of
these functions without params.
*/
// send data to script.php to save data
$("#div").load("script.php?str="+list_data_str);
$.cookie("list_data", list_data_str);
}
});
}
Edit #2 / Solution:
I don't know why, but everything works with window.onbeforeunload instead of jQuery.unload(). An explaination would be great! I'm sorry for this confusing thread!
window.onbeforeunload = function(e) {
$("#div").load("script.php");
}
I think that your issue is with: list_data_str as it's not defined anywhere.
if you are trying to say that you want to do AJAX post for example, then obviously you need to look for success event
else it appears that what your demo code is missing something because you can do it the way you are trying if at the receiving script you use $_GET over the URL and not pay attention to any parameters.. In other words, you are missing the object and when you refresh the page it's loaded into the DOM. Apparently that could be the issue that you are describing, I would suggest that you post a bit more of relevant to your issue code.. like the receiving script or any errors from a debugger like Firebug.
Regarding how to test it, you might want to use console.log in supported browsers or simply alert when is setting up the cookie.
var global list_is_dirty = false;
function sort_list(list, list_is_dirty) {
// some code, not important
//check the list and the flag
//you should return a value, else it does not make sense to use a function here.. note the var defined as global
return list; //?? not sure what to return as don't know what this code does from the posting
}
jQuery(function($)
{
$(window).load(function(e){
var list_data_str= sort_list( );
// send data to script.php to save data
e("#div").load('script.php?str='+list_data_str);
//on unload destroy the cookie perhaps?? or if it's not set a session variable
e.cookie("list_data", list_data_str);
...
The unload event
$(window).unload(function(e) {
$("#div").load("script.php?str="+list_data_str);
$.cookie("list_data", list_data_str);
}
});
}
....
// About your EDIT: Are you passing in here any parameters to the script? Because I think the problem is at that logic.
window.onbeforeunload = function(e) {
$("#div").load("script.php");

Categories