So I have a script on one part of my domain that sets the localStorage:did to true, and then send them to another part of my domain.
The page, which the viewer was sent to, checks to see if the did is true, and it is, but for some reason the function that starts if did is true does not work (but I have used that function a few days ago, so it works).
$(document).ready(function() {
var is = localStorage.getItem('did');
if (is == 'true') {
localStorage.setItem('did','false');
window.alert("ok")
$.get('give.php' , {} , function(response){
//window.alert(response);
});
} else {
window.alert("no")
}
});
This is the checker. I don't think there is something wrong with it, but if I am mistaken, please tell me.
localStorage.setItem('did','true');
This is from the original page which sets did to true. I'm pretty sure that's how you do it.
When logging did on the other page, it comes out as true, but why is the if function not firing?
Related
EDIT: the issue has been resolved. Apparently, the page didn't load fast enough for the id referenced to to be in place. Adding a event listener as follows solved the issue:
window.addEventListener('load', function() {stuff that's supposed to happen})
on my clueless journey to creating a little game I'm currently battling saving to and loading from the user's local storage. While saving appears to be working, loading doesn't, unless I manually put the respective commands into the console.
Here's the code snippet that's acting up:
//definition of variables
var dayLength = 10;
//if function to check if there's stored data, loading it and changing the document accordingly
if ( localStorage.saveExists === "true" ) {
dayLength = Number(localStorage.dayLength);
document.getElementById("displayDayLength").innerHTML = dayLength.toFixed(3);
}
var loadingAttempted = "true";
//function to save which occurs every 15 seconds (or through a button click)
function saveAuto() {
if ( loadingAttempted === "true" ) {
var saveExists = "false";
localStorage.saveExists = "true";
localStorage.dayLength = dayLength;
}
When no data are in the local storage (in other words, if saveExists = "false") the site won't attempt to load something and works as intended. The moment there is data in the local storage, the file attempts to load it (so thus far it works).
However, I'll get an error in the line supposed to change the document according to the loaded data, claiming that dayLength is "null".
If I then manually input localStorage.dayLength into the console, it returns "10", as it should. I know that storing the data into the local storage works, because otherwise the if function for loading wouldn't know (saveExists === "true") and thus not do anything. For whatever reason though, it fails to load any other data correctly and returns "null", unless I manually put the exact same code into the console.
Best part is, that I got the whole idea from another little game that does the exact same thing and there it works flawlessly.
I'm at a total loss as to why it works at the other project, but not with mine.
Educate me, if you will. And thank you in advance.
PS: I tried to use localStorage.setItem and localStorage.getItem also, but the issue remained.
I'm trying to create a chrome extension with the 'background' permission, meaning it continues running in the background even if the chrome window is closed. However, if the user ends chrome in the background as well, I want to reset some values in chrome.storage to default values. I thought creating a listener for the onStartup event would work, where I would then reset these values. However, this doesn't work. For whatever reason, chrome.storage.sync.set doesn't work properly in the callback of onStartup.
When checking the log, I always see 'Startup', therefore it's definitely running without a problem as far as I can tell, though if I later check the values they remain the same as before closing Chrome completely.
I've already checked through my code to see if the value is changed somewhere else but this does not seem to be the problem. To be certain I've tried commenting everything else out, but it still doesn't work.
Here is the code I have right now (it's in my only background.js file).
chrome.runtime.onStartup.addListener(function() {
chrome.storage.sync.set({
desc: '',
timer: -1
}, function () {
console.log("Startup");
});
});
chrome.runtime.onInstalled.addListener(function() {
chrome.storage.sync.set({
desc: '',
timer: -1
}, function () {
console.log("Installed");
});
});
For clarity, here is an example inside my popup.js file where the value 'desc' is changed depending on user input. Whatever is written here is the value that remains even after onStartup has run, which is not expected:
i_text_pom.oninput = function() {
chrome.storage.sync.set({desc: i_text_pom.value}, function () {
console.log("Uus desc on: " + i_text_pom.value);
});
};
I expected the storage variable 'desc' to be just an empty script, but once chrome has restarted it's still the same as before the full restart.
I am debugging the weirdest error I ever seen. At this point my code look like this.
var counter = 0;
function setup() {
var count = ++counter;
var test = false;
function getSetter(arg?) {
if (typeof (arg) !== "undefined") {
console.log(["setting",count, arg]);
test = arg;
} else {
console.log(["getting",count, test]);
return test;
}
}
return getSetter;
}
var verticalScrollDisabled = setup();
this is in a closed scope and i have made sure that the variable test is not accessed outside the above code. I can change it to any name with same result. Updated such its clear that its not accessed outside of this scope. And updated with a counter to show its not written over.
Copy pasting result from the console.
["enter scroll area", div.fxs-blade-content, true, "3511>401 || 577>585"] HorizontalScrollBindingHandler.ts:12
["setting", 1,true] HorizontalScrollBindingHandler.ts:132
disable vert HorizontalScrollBindingHandler.ts:72
n.Event {originalEvent: WheelEvent, type: "mousewheel", isDefaultPrevented: function, timeStamp: 1422128039040, jQuery21104183536011260003: true…} HorizontalScrollBindingHandler.ts:15
["getting", 1,false] HorizontalScrollBindingHandler.ts:15
["getting", 1, false] HorizontalScrollBindingHandler.ts:75
[false, false]
Issue
As commented, my problem is that as seen in the trace. the variable get set to true, but when its being accessed again its false. I cant get why that can happen.
and the handler attached to mousescroll event.
var scrollHorizontally =(e) => {
// console.log([verticalScrollDisabled(), scrollInAction]);
console.log(e);
if (verticalScrollDisabled() && !scrollInAction)
return;
console.log([verticalScrollDisabled(), scrollInAction]);
This code has been working for ever and nothing changed to it other than we in some seperate code are opening a popup and closing it again. Is there anything that could cause events to be doing something out of the expected related to if the window loses focus or something? Again, the test variable is not altered outside the verticalcrollDisabled function, so I have no clue why it can go change itself to false, notice the ["setting", true].
Just verified that the popup is not the cause.
Heres the hole file. https://gist.github.com/s093294/e49ed46d2680c1403e3b
Answer is, validate your assumptions.
I assumed that the file was not loaded twice since it being a module defined with define and loaded with requirejs. This didnt hold up since there was some ID mapping that made two ids load this file and as one of the comments said, this is the thing that makes it possible.
I resolved the requirejs configuration and problem vent away.
I have a form. It contains some hyperlinks. I can click on them and get another page in browsers like Google Chrome and Mozila Firefox but instead, I am unable to open them in IE9, IE10,..
What might be the problem? and whats the solution? help me.
In dev-tool(console), I am getting
un-terminated string constant error
.
Apart from any coding this could be the reason:
A previously installed browser or add-in might be interfering with ie browser on your computer.
or it might be a result of bad DCOM.
If bad DCOM is the problem you can get solution here:- http://www.techsupportall.com/links-are-not-working/
Jay,
This might happen due to incorrect concatenations or omission of the semicolon(;) at the end of statement.
Or may be any server variable that is not being populated. Like:
var var1 ='<% = someServerVariable %>'
someCode
It is possible that some someServerVariable is not being populated and the browser compiler would read the code as
var1 = someCode
Please look into your code and find out if there is any similar issue with your code. Or the best is to share your code snippet to point out the exact issue.
I ran into this same exact problem. For me, whenever I was embedding links in an ul / li list format, it just wouldn't let me click the first link (happens a lot navigation menus).It would only let me click the embedded ones. My work around for this was using java-script to create a force the click and pass.
Put this script in your head
<script>
function fakeClick(event, anchorObj) {
if (anchorObj.click) {
anchorObj.click()
} else if(document.createEvent) {
if(event.target !== anchorObj) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var allowDefault = anchorObj.dispatchEvent(evt);
// you can check allowDefault for false to see if
// any handler called evt.preventDefault().
// Firefox will *not* redirect to anchorObj.href
// for you. However every other browser will.
}
}
}
</script>
And then in the body you can use this convention for any link you need to be forced clicked.
<a id="link" href="#YourDestinationLinkHere" onclick="fakeClick((event.target || event.srcElement).innerHTML)">Destination</a>
I've inherited a Facebook app, which doesn't work 100% as intended. I don't understand the logic of the previous developer, but I'm not strong enough in the Facebook API to rewrite it.
The app has a form, in which the user (when logged in) needs to answer a few questions. When he clicks submit, we want to check whether or not he likes the page. The form will only be submitted if he does.
window.fbAsyncInit = function() {
FB.init({
appId : '[myappid]',
channelUrl : '//www.myurl.com/channel.aspx',
status : true,
cookie : true,
xfbml : true
});
};
$("form input[type='submit']").on("click", function(e) {
e.preventDefault();
setTimeout(function() {
FB.api('/me/likes/MY-PAGE-ID',function(response) {
if(response.data) {
alert(response.data); // I get this (empty) alert
if(!isEmpty(response.data)) {
alert('all ok'); // I never get this :(
//$("form").submit();
} else {
alert("You must \"Like\" the page."); // Every time
}
}
});
}, 1000);
// Unrelated code edited out - end brackets may not match 100%
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
Now, on desktop this works as intended. However, on Android it doesn't. It does enter the first if(response.data) { but the following alert box which should return the data, returns empty. It therefore never enters the following if-statement.
The part I don't understand, is why it enters the first if (response.data exists) but not the second (because the response is somehow empty, even if the page is liked).
I don't have a different device to test on, sadly.
Am I overlooking some obvious glaring mistake? What can I do to salvage it?
I figured it out. It seems I was not asking for the necessary permissions, because I thought "likes" was part of the basic info. Silly of me, I realize.
FB.login(function(response) {
// Truly amazing code goes here
}, { scope : 'user_likes' }); // Scope is important, yo
What I don't understand, is why it worked on desktop, but not on mobile, but I'm happy now that it works.