This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
(14 answers)
Closed 3 years ago.
Hello gurus in the house. It sounds like a duplicate but I am having a tough time with it since 48hrs past. The question is based on a jQuery. I have a file that i would like to pass into a variable then later use the variable in other places of my code. The file only contains 'I love you'.
<script>
var kk;
$.get('k.txt', function(data) {
kk = data;
}, 'text');
alert(kk); //undefined
</script>
The result of the alert is undefined and it does not work in opera browser. I can alert it like this.
<script>
var kk;
$.get('k.txt', function(data) {
alert(kk); //alert is fine
}, 'text');
</script>
I don't mind if you can show me any other working process apart from this. Please I need this so badly and I have being dealing with this since yesterday. Thank you sirs for understanding.
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
This line of code is getting document count from firestore database it works fine when i call the variable inside the function but outside its undefine.
var LM35TOTALRES; // I WANT TO STORE HERE
db.collection("LM35").get().then(function(querySnapshot) {
LM35TOTALRES = querySnapshot.size //DATA THAT I WANT
console.log(LM35TOTALRES); // THIS IS WORKING
});
console.log(LM35TOTALRES); // NOT WORKING
I believe this is because the function is async so it finishes setting the value after the log outside, One solution is to return the value needed in the variable directly and await it like so const LM35TOTALRES = await (db.collection("LM35").get()).size
This occurs due event loop scheduling you can find some good examples in this video about min 2 or so
use let instead of var
let LM35TOTALRES;
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Accessing $_COOKIE immediately after setcookie()
(9 answers)
Closed 3 years ago.
I am creating ajax model update request, after this I want to refresh my compacted variable with ajax response. I need to do these tasks without page reload. If anyone have any ideas please share with me.
My code:
success: function(result) {
console.log(result);
var abc = JSON.stringify(result);
Cookies.set('clients', abc, 10);
$('#cookie').html(Cookies.get("clients"));
var abc2 = $('#cookie').text();
<?php $client1 = $_COOKIE['clients']?>
I have been trying to do it with cookies, but as I know, to get recent cookies need reload page.
This question already has answers here:
What is the result of this javascript?
(3 answers)
Closed 5 years ago.
can you tell me what this code does as I need to understand it for a coding assessment. I only just started learning JavaScript
function getAttackString() {
var foo = "d323b8b34";
var bar = "x334q3j98";
return "The code is: "+(foo.substr(3,foo.length-6))+(bar.substr(2));
}
Thanks for your help
Start by looking up what .length and .substr are.
For example, what will foo.length give you? A good way to find out would be:
var x = "abc123";
console.log(x.length);
console.log is a command to print information to your web browser console, very helpful when doing debugging work.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Here is the JSON I am trying to parse
{"sites":{"1":{"id":1,"company":"facebook","username":"abc#gmail.com","hint":"mascot"}}}
Here is the javascript parsing it. It was working then the structure of the JSON changed and I cannot for the life of me get it work. The result is 'undefinedundefined' which means that it does not understand what username and hint are. Any thoughts?
$.getJSON('http://localhost:3000/sites.json', 'limit=30', processWebsites);
function processWebsites(data) {
var infoHTML='';
$.each(data, function(website, websiteDetails) {
infoHTML+= websiteDetails.username
infoHTML+= websiteDetails.hint;
});
$('#info').html(infoHTML);
}
and finally the HTML
<body>
<div id = "info">
</div>
</body>
i needed a .sites after data. this fixed it.
I believe felix is correct above. If you just console this, you'll get the value you need.
update: add your breaks
var infoHTML = [];
var data = {"sites":{"1":{"id":1,"company":"facebook","username":"abc#gmail.com","hint":"mascot"}}};
jQuery.each(data.sites, function(){
infoHTML.push(this.username + '<br>' + this.hint);
});
If you happen to get more than one grouping, then just join them with 2 br's if you want some seperation.
$('#info').html(infoHTML.join('<br><br>'));
I am sure there are much more eloquent ways. this is just fast and dirty.
This question already has an answer here:
about chrome.tabs.executeScript( id,details, callback)
(1 answer)
Closed 8 years ago.
I am injecting a script into a tab using chrome.tabs.executeScript. Its last parameters is a callback function that will be called with The result of the script in every injected frame. . How do I make my script return a result? I tried adding a simple return statement at the end of my js, but nothing got returned.
As RobW corrected me, a function is not necessary, just make sure that the desired data is the last expression and of course that is JSON-serializable.
chrome.tabs.executeScript(tabId,
{code:"document.charset"},
function(results){
// results[0] will now contain the charset for the page in question
});