I've been toying with this for far too long...
function OnSearch() {
var text_in = sVal()
var tArea = document.getElementById("SearchResults")
var loadstr = "Loading results for "
var xhttp = new XMLHttpRequest();
var altStr = ""
var sendstr = ""
var urls = []
for (i = 0; i < ciphersOn.length; i++) {
aCipher = ciphersOn[i]
sendstr += "/search="+text_in+""
sendstr += '/name=' + aCipher.Nickname + ''
sendstr += '/letters='
for (x = 0; x < aCipher.cArr.length; x++) {
sendstr += String.fromCharCode(aCipher.cArr[x])
}
sendstr += "/cipher="
for (x = 0; x < aCipher.vArr.length; x++) {
if(aCipher.vArr.length == x+1){
sendstr += aCipher.vArr[x]
} else sendstr += aCipher.vArr[x] + "-"
}
/* send http GET and bring back info to a new list, then clear sendstr before it loops again. */
urls.push(sendstr)
sendstr=""
}
for(i = 0; i < urls.length; i++) {
xhttp.open("GET", "http://localhost:8000" + urls[i] + "", true);
xhttp.send();
}
loadstr += '"' + text_in + '"' + sendstr + '' + urls.length + '' + aURL + '' /* the purpose of this is so I can see what is happening to my code */
tArea.innerHTML = loadstr
}
I have no clue why that for loop at the end only sends one GET request. Please, spare me my sanity... I just don't get it. The array "urls" contains the information I need, and the variable "sendstr" works perfectly well... Why then does my terminal only show that the first result is being given?
Each XMLHttpRequest can only send one request. As stated in MDN, calling open on an already open request is equivalent to aborting the request. So, create a new XMLHttpRequest for each loop:
for(i = 0; i < urls.length; i++) {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://localhost:8000" + urls[i], true);
xhttp.send();
}
Alternatively, migrate to fetch:
for(i = 0; i < urls.length; i++) {
fetch("http://localhost:8000" + urls[i]);
}
Related
I have a problem with some code. I started building a plugin for Chroma to insert data using JSON, but NaN is displayed to me.
My file content.js
var xmlhttp = new XMLHttpRequest();
var url = "http://rlinkit.neteasy.pl/abc.json";
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myArr= Array();
var myArr = JSON.parse(this.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
for (i in arr) {
for(j in arr[i] ){
for(p in arr[i][j]){
out = arr[i][j][p].currency + arr[i][j][p].code + arr[i][j][p].bid + arr[i][j][p].ask + '<br>';
}
}
}
document.getElementById("id01").innerHTML = out;
}
My CodeHTML:
<div id="id01"></div>
<script src="content.js"></script>
My Output:
Edit (When I give the code - nothing is displayed.):
function myFunction(arr) {
var out = "";
var i;
var j;
var p;
for (var i = 0; i in arr.length; i++) {
for(var j = 0; j in arr[i].length; j++ ){
for(var p = 0; p in arr[i][j].length; p++){
out += arr[i][j][p].currency + arr[i][j][p].code + arr[i][j][p].buy + arr[i][j][p].sell + '<br>';
}
}
}
document.getElementById("id01").innerHTML = out;
}
To iterate over a JavaScript array, either use a for ... of loop or a traditional for loop. The syntax in your updated function (specifically, i in arr.length) is invalid JavaScript (looks like Python?).
Providing example data would help to improve this answer, but try some version of the following (updated to use ES6 let syntax, which you may want to revert to var):
function myFunction(arr) {
let out = "";
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
for (let p = 0; p < arr[i][j].length; p++) {
out += arr[i][j][p].currency + arr[i][j][p].code + arr[i][j][p].buy + arr[i][j][p].sell + "<br>";
}
}
}
document.getElementById("id01").innerHTML = out;
}
I am trying to create a JavaScript function where I want to modify the object that I pass in and return that. I am able to create a string of the full path of the content that needs to be changed, but this is string. How can I change this so that it recognises that it is a JSON Path?
Currently, it fails my if check as it's checking as a string, when I need it to check as a JSON Path.
var modifiedObj = function (content) {
for (var i = 0; i < content.length; i++) {
var pathMaker = ["randonPath.one", "randonPath.two", "randonPath.three", "randonPath.four", "randonPath.five"];
for (var j = 0; j < pathMaker.length; j++) {
var pathValid = "content[" + i + "]." + pathMaker[j] + ".valid";
var pathNotValid = "content[" + i + "]." + pathMaker[j] + ".notValid";
if (pathValid !== undefined && pathNotValid == undefined) {
pathNotValid = content.referenceAnotherField;
};
};
};
return content;
};
Any advise?
i want to reverse the order of this list using javascript i have tried different ways that i knew about it is
the below file show straight i want to reverse it.i dont know xml node much and how to get it reverse it totally
<messages>
<messageset>
<name>torje</name>
<time>1533904431</time>
<message>Vvvhjhf</message>
</messageset>
<messageset>
<name>moneyman</name>
<time>1533904437</time>
<message>njkjlmkmkl</message>
</messageset>
<messageset>
<name>anjali</name>
<time>1533904445</time>
<message>A hi fyi bk MLS egg FG ch bhi CDG jk IC</message>
</messageset>
</messages>
it the present code this shows order wise table like torje - Vvvhjhf , moneyman - njkjlmkmkl, anjali - A hi fyi bk MLS egg FG ch bhi CDG jk IC this should be reverse first anjali's msg then moneyman then torje's (sorry for bad typing in message)
function fetch() {
setTimeout( function() {
loadDoc()
fetch();
}, 100);
}
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "../clubs/club.xml", true);
xhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>NAME</th><th>message</th></tr>";
var x = xmlDoc.getElementsByTagName("messageset");
for (i = 0; i < x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
Can create an array and push each data row into the array then Array#reverse() that array and convert back to string using Array#join()
function myFunction(xml) {
var rowsArray =[]
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>NAME</th><th>message</th></tr>";
var x = xmlDoc.getElementsByTagName("messageset");
for (i = 0; i < x.length; i++) {
// new variable `row`
var row = "<tr><td>" +
x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue +
"</td></tr>";
// add row to array
rowsArray.push(row)
}
// reverse and return array to string
table += rowsArray.reverse().join('');
document.getElementById("demo").innerHTML = table;
}
Currently your for loop processes the messages in standard order.
for (i = 0; i < x.length; i++) {
You could just reverse this loop:
for (i = x.length - 1; i >= 0; i—-) {
I have created a related post javascript for blogger.com based blogs. The related post plugins loops though the label attached to the current post and displays other posts with the same label.
You can find that in action # http://www.techquark.com/2017/07/infocus-turbo-5-affordable-powerful.html
The issue is I want to skip the current post to be shown in the related post, but unable to achieve the same.
PFB the javascript code :
$(".related-ready").each(function() {
var b = $(this).text();
$.ajax({
url: "/feeds/posts/default/-/" + b + "?alt=json-in-script&max-results=3",
type: 'get',
dataType: "jsonp",
success: function(e) {
var u = "";
var h = '<div class="related">';
for (var i = 0; i < e.feed.entry.length; i++) {
for (var j = 0; j < e.feed.entry[i].link.length; j++) {
if (e.feed.entry[i].link[j].rel == "alternate") {
u = e.feed.entry[i].link[j].href;
break
}
}
var g = e.feed.entry[i].title.$t;
var c = e.feed.entry[i].content.$t;
var $c = $('<div>').html(c);
if (c.indexOf("//www.youtube.com/embed/") > -1) {
var p = e.feed.entry[i].media$thumbnail.url;
var k = p
} else if (c.indexOf("<img") > -1) {
var q = $c.find('img:first').attr('src');
var k = q
} else {
var k = NO_IMAGE
}
h += '<li><div class="related-thumb"><a class="related-img" href="' + u + '" style="background:url(' + k + ') no-repeat center center;background-size: cover"/></div><h3 class="related-title">' + g + '</h3></li>'
}
h += '</div><div class="clear"/>';
$(".related-ready").html(h);
$('.related-img').each(function() {
$(this).attr('style', function(i, src) {
return src.replace('/default.jpg', '/hqdefault.jpg')
}).attr('style', function(i, src) {
return src.replace('s72-c', 's1600')
})
})
}
})
});
TIA !
You will need to check whether the current Post's ID matches with the ID of the post in feed and then skip that post, the code will look like -
....
for (var i = 0; i < e.feed.entry.length; i++) {
if(e.feed.entry[i].id.$t.split("post-")[1] === "<data:post.id/>"){
break;
}
for (var j = 0; j < e.feed.entry[i].link.length; j++) {
....
Javascript:
function add_content(count)
{
var result = {};
var row = new Array();
for (i = 0; i < count; i++)
{
result['img_src'] = $("#img_" + i).attr('src');
result['desc'] = $("#desc_" + i).val();
row.push(result);
}
$.ajax({
url: "ajax-functions.php",
data: {json_data: JSON.stringify(row)},
type: 'post',
success: function (output) {
alert(output);
}
})
}
Php:
$img_desc = json_decode($_POST['json_data'],TRUE);
$count = count($img_desc);
echo 'count:'.$count;
print_r($img_desc);
I want to send json to from JS to PHP. This above code works well, but it send the last data of the for loop is set in all the objects.
How to fix it? Is this right way?
Thanks.
Push a new object on each round of the loop into to the array
for (var i = 0; i < count; i++) {
row.push({
"img_src": $("#img_" + i).attr('src'),
"desc": $("#desc_" + i).val()
});
}
fiddle
Move result declaration inside for:
var row = new Array();
for (i = 0; i < count; i++)
{
var result = {};
result['img_src'] = $("#img_" + i).attr('src');
result['desc'] = $("#desc_" + i).val();
row.push(result);
}
or it can be done more elegantly:
var row = new Array();
var result;
for (i = 0; i < 10; i++)
{
result = row[ row.push({}) - 1 ];
result['img_src'] = $("#img_" + i).attr('src');
result['desc'] = $("#desc_" + i).val();
}
You can make the keys of result array also dynamic
Since you are not making it dynmamic, its getting over ridden in the next loop
EDIT:
for (i = 0; i < count; i++)
{
var result = {};
result['img_src'] = $("#img_" + i).attr('src');
result['desc'] = $("#desc_" + i).val();
row.push(result);
}