PhoneGap localStorage taking too long to access data - javascript

I have saved long string in localMemory named UserContacts, but it takes very long to access the data, here is the code that I am using:
var $Contacts = $('#Contacts');
var htmltext = window.localStorage.getItem("UserContacts");
$(htmltext).appendTo($Contacts);
$Contacts.listview("refresh").listview();
Any ideas how to improve performance for this?
P.S. I am developing application for android and using PhoneGap.
Thank you.

instread of storing html in localstorage you should save only data and then create your html using javascript, see below code
var data = window.localStorage.getItem("UserContacts");
var len = data.length;
var html = "";
for(var i=0; i< len;i++) {
html += $("<li/>").text(data[i]);
}
$("ul#yourId").append(html)

Related

Error saving this JS in WSO2-Integration Studio

I use Integration Studio,
and I have always used inside my Xml files js script,
But this time I still get an error saving this inline JS, and I really can't understand the reason of this error.
<script language="js"><![CDATA[var elements = mc.getProperty('elementsFromJson');
var payload = [];
for(let i = 0; i <= elements.length; i++){
var id= elements[i].id;
var type= elements[i].type;
var couple= {"id":id ,"type":type};
print(couple);
payload.push(couple);
}
var payloadFinale = {payload};
print('result in JSON: ' + payloadFinale);
var data= mc.setPayloadJSON(payloadFinale);]]></script>
So would be great your help in case you use Integration studio as me,
thank you for your time.
You have issues in the following lines.
for(let i = 0; i <= elements.length; i++){
Here change let => var
var payloadFinale = {payload};
The above syntax is wrong, not sure what you are trying to achieve here. Probably, you can remove the above line, when you do mc.setPayloadJSON it would be saved as a proper JSON.

How to access cell data from iframe Google Sheets

I'm trying to pull two specific numbers from a chart on a football website (Column PF, Row "Team Stats" and "Opp. Stats") and import them to my website as a JavaScript variable which I can do math, etc. with. My solution thus far since I don't want to get into anything too crazy has been use the IMPORTHTML function in Google Sheets, create a table, then export that bit of the spreadsheet using the "publish to web" feature which condenses it into an iframe.
I've tried using object properties of the iframe to access it including a getElementbyId dot property among others.
<h1>
<script>
var iframe = document.getElementById("data");
var elmnt = iframe.contentWindow.document;
//.getElementsByTagName("table")[0];
document.write(elmnt);
window.parent.document.getElementById('data')
</script>
</h1>
I was hoping for an output of the correct number but am getting [object HTMLDocument] or undefined depending on the properties I'm trying
Why don't you use the Sheets API ?
Using the api, you would use this simple command :
var result = Sheets.Spreadsheets.Values.get(spreadsheetId, range);
var numRows = result.values ? result.values.length : 0;
but the setup is a bit heavy
If you dont want to you can still serialize table data.
Here is the work of John Dyer :
function tableToJson(table) {
var data = []; // first row needs to be headers var headers = [];
for (var i=0; i<table.rows[0].cells.length; i++) {
headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');
}
// go through cells
for (var i=1; i<table.rows.length; i++) {
var tableRow = table.rows[i]; var rowData = {};
for (var j=0; j<tableRow.cells.length; j++) {
rowData[ headers[j] ] = tableRow.cells[j].innerHTML;
} data.push(rowData);
}
return data;
}
the you'll can use data[column][row] to get the cell data;
In my opinion using the API is the best option in this specific case.

MD5 of a file in JS and in Python/Django

I need to check the md5 of an image in django and in javascript. However I don't obtain the same results. The django code give me the same md5 as md5sum on a terminal.
Django code :
file0 = request.FILES.get('file')
buffer0 = file0.read()
print hashlib.md5(buffer0).hexdigest() //9f982242d24ab97a7254edd7e28e3921
I tried two javascript md5 library, which give me the same results ( https://github.com/blueimp/JavaScript-MD5 and https://github.com/sytelus/CryptoJS ).
I have tried different methods (readAsArrayBuffer, readAsBinaryString, ...) and none of them give me the same md5 as python and md5sum.
Javascript :
var reader = new FileReaderSync();
var datas = reader.readAsArrayBuffer(file);
console.log(md5(datas)); //060e4e9e30bcb9ae675a80328a87a687
var string0 = reader.readAsBinaryString(file);
console.log(md5(string0)); //2e4cac0a23ddf95683c6538d64b26e21
console.log(CryptoJS.MD5(string0).toString(CryptoJS.enc.Hex)); //2e4cac0a23ddf95683c6538d64b26e21
var string1 = reader.readAsText(file,'ascii');
console.log(md5(string1)); //329c4271b8eda786213b2468e378b251
console.log(CryptoJS.MD5(string1).toString(CryptoJS.enc.Hex));//329c4271b8eda786213b2468e378b251
var view = new Uint8Array(datas);
var str = ""
for (var i=0, strLen=view.length; i < strLen; i++) {
str+= String.fromCharCode(view[i]);
}
console.log(md5(str)); //2e4cac0a23ddf95683c6538d64b26e21
console.log(CryptoJS.MD5(string1).toString(CryptoJS.enc.Hex));
I think the problem come from how I read the file in JS.
I used SparkMD5 (https://github.com/satazor/js-spark-md5) which can worked directly on an array buffer and it give me the same md5 than in python.

collect data by id using javascript

I have to save temporary data for my webpage using java script.This is the way that i save i one by one since the data is an array.
var product= new Array();
product[1] = document.getElementById("product[1]").value;
product[2] = document.getElementById("product[2]").value;
This method is working. but when i run it by looping, it doesnt work.
for(var i=1; i < no_item; i++){
product[i] = document.getElementById("product[i]").value;
}
*product[] is a varibale that I take from a html dropdown menu
Can anyone please tell me the problem ? thanks ~ =)
Should be written as, as you are going to be getting the id "product[i]" every time with your original code. This will get "product[1]" then "product[2]" and so on:
for(var i=1; i < no_item; i++){
product.push(document.getElementById("product[" + i + "]").value);
}
Also, as a comment, we tend to prefer var product = []; over var product = new Array(); in javascript but both will work.

convert js output to objective C

I am doing work on iphone app in which I have also used js as well as css files along with objective C to get better output. But at one point I stuck out. I am using the following js code which is showing the alert. I have attached the image also which is representing the alert by clicking on OK button.
**NSString *someHtmlContent1 = #"<script src='jquery.min.js'>
</script>
<script src='jquery-ui.min.js'></script>
<script src='jquery.ui.touch-punch.min.js'></script>
<script>
function result(qn) {
var u = document.getElementById('sortable'+qn);
var LIs = u.getElementsByTagName('li');
var list = [];
for( var i = 0; i < LIs.length; ++i ){
var LI = LIs[i];list.push(LI.innerHTML);
}
res = list;
var i=0;
var str = '';
for (i in res) {
str = str+' '+res[i];
}
alert(str);
return str;
}
</script>";**
I want to get the content**(.......)** in NSString which is showing on alert. Anyone know that how to convert js variables to objective C? Please do help me.
Thanks to all.
The only trick to make a link between JS and objective-C is the following one :
In your JS, call a link like window.location = "something://smth/smthelse"
And in the Objective-C, in your web view delegate method (webView:shouldStartLoadWithRequest:navigationType: ), you can handle this call and do something with it.

Categories