Storing large but simple array in Javascript? - javascript

Im trying to solve a problem for restricting the display of certain shipping methods on a web store and a little stumped on the best way to accomplish.
I have a semi large (43000 row) csv file that consists of 2 columns. Zip Code and Day in transit. At the "estimate shipping" stage of checkout I need to query this data via javascript. Im taking the shipping address zip code and using it to look up its corresponding days in transit and return that value for some further calculations. Normally I would load this into a database table but this store is on BigCommerce where no direct DB access is available so Im assuming this has to get done client side without setting up a remote DB.
What would be the best way for me to store that information if it had to be client side? My original idea was to create a giant array and store it in a JS file on the server that would be included when needed. HTML localstorage? Any guidance is appreciated!

With localstorage you would get an 5mb per domain limit, I am not sure if that is enough to store the array you want.
Another way would be to convert that array to a .json file and use ajax/get in order to retreive it (GET will be cached so only the first time it will load the file from the server, then it will go much, much faster as it will load it from the client cache).

Related

Can we write to file from React?

Currently I'm developing a React web application that needs to store some data from user in the client side and also this data are Important and should not swap suddenly so we can't use Indexed DB.
I just thought about using a file to store data as JSON but in a file that can be stored in user computer and also access to read and write it from React.
The best way to work with that is using localStorage.
Cookies are mainly for reading server-side, and local storage can only be read by the client-side. Other point is saving data, a big technical difference is the size of data you can store, localStorage give you more space.
Check it out https://medium.com/#siobhanpmahoney/local-storage-in-a-react-single-page-application-34ba30fc977d
What you're looking for is the in-browser sessionStorage or localStorage. Having the ability a JSON file to a user's computer would be a major vulnerability and is not possible from a browser/client. I suppose you could create one and trigger a download for them to accept–and then have them subsequently re-upload it when you need the data again. But, I'd argue that session/localStorage is more what you're looking for.

Connecting local HTML files to Sharepoint database

I am new to using this stuff. I need to make some sort of a connection from a local html file on my computer to either Sharepoint or an Access database. I want to be able to pull data from the Sharepoint/Access db and display it on my html page. I also would like to use javascript to accept user input and in turn query those databases with that.
I currently have built some simple vba that outputs an HTML file with table data and another HTML file which houses that table with an iframe on the page. This is very limited and does not provide full functionality. Can anyone lead me in the right direction? Is this even possible without those pages being hosted on a server somewhere? I understand I may need to use PHP, but I do not believe I can use that functionality without having these files on a server.
Forgive me if I do not sound like I know what I'm talking about...I don't really :)
Thanks!

Is it possible to generate a text or xml file from local storage data and save it in a folder?

I'm working on a website that is going to be offline. All the html files will be in a folder stored on the hard-disc. I've managed to do 90% of the work and the last part I have no idea of. Here is what it is:
I have stored a list of products in the localStorage as various strings under the keys - like buying objects and it goes to the cart, the cart objects are in localStorage. I created a page that showed the list of all the products in the localStorage. It can be cleared if the user clears them. Now I need to create a page where all the objects that was selected before, regardless of the localStorage being cleared, show as list in this page. You can take it as the page that lists products that have been ordered in the past, i.e even after the cart is cleared the products will show in the past-orders page.
I do not know any server side codes, I did everything using JavaScript as it was supposed to be a simple project, but I'm stuck at this part. So I cannot use PHP or anything to generate files or use a database to store stuff.
Here's what I thought but I don't think it works but wanted to confirm if it does or not:
Generate an XML file or a .txt file and store it in the drive and then clear the localStorage. But I don't think it is possible. If its possible just using JavaScript please point me in the right direction and I'll research and come up with something.
P.S. the website will be entirely offline what I mean is the users will never connect to the internet for this to work. Also there won't be a server or localhost.
Thank you!
The site is completely offline, but functionality is similar to an eCommerce site. You click a button and some content from the website stores in the localStorage and I have to call it in multiple pages, when a user clicks another button, localStorage clears but whatever was selected before must be available without localStorage. Hmmmm.. Consider a quiz site where you answer everything and when you take a new quiz, old scores will be stored somewhere else and it won't change when you take a new test.
Is it possible to attain this functionality without a server side script? It seems the final-targeted-users won't know how to install any softwares that can provide a localhost or a server or something like that.
Client-side, browser's JavaScript runtimes don't have local file system access excepting a very limited one when uploading files to a remote server, and anyway, browers won't give you direct access to the file.
You'll need some kind of server-side logic behind the scenes if you want full I/O in the local file system.
Perhaps you can take a look at NodeJS, a server-side JavaScript runtime that can work as lighty Web server on either small and large projects, and it has built-in server-side JavaScript I/O functions.
Otherwise, you're absolutely stuck in the client-side Web browser's sandbox limitations.
U can refer documents of knockoutjs and NodeJS.. That would probablky help... A far as my knowledger is concerned NodeJS does contain a way to handle your problem.

Angular: hide JSON data from user

My Angular application loads JSON files as search material using $http
$http.get('my.json').success(function(data){ ... };
Unfortunately, this method loads the JSON file in to the browser and allows the user to see it's content.
Screenshot from Safari
I'd like to hide that file from the user.
To refine my question. I know that I won't be able to totally hide that data from the user.
A user can easily write a script to scrape the data off my site.
I just don't want to hand those files to the user so easily, to just let them sit there for him to just copy and paste.
Everything you sent to client is anyhow visible by client. There is no way to absolutely hide it from him.
All you can do is .zip or encode data on server and unzip or decode it on client. But even in this case all decoding mechanisms will be available to user and if he wishes and has some skills, he can decode it and access all data you send him.
UPD:
Ok, you want to uglify your data so client couldn't see it simple way.
You need to somehow encode data on server, send it encoded to the client, and decode on client so you could use it.
Checkout lz-string - it's js library that can archive / unarchive json strings, converting them to binary format, so data becomes unreadable. And it's pretty fast (I haven't noticed any delays encoding/decoding 2Mb strings - I use it to compress data and store it in localStorage).
It's as easy as LZString.compress(JSON.stringify(data)) to compress json and JSON.parse(LZString.decompress(data)) to decompress.
Only thing you need in addition - to find a server-side library that uses exactly this format. If you use NodeJS at your server, you can take lz-string itself. Otherwise, quick googling gave me solution for php - I think it's pretty possible to find solutions for other languages..
If you want the user to see only parts of the document, you should filter it on the server.
Create some kind of service/Api-function/etc. which loads the document and pass it the user information via ajax request. Now filter it depending on this user information and return the filtered data.

How to store XML file on client's system and access it through JavaScript

The problem is- I have a manage reservation page where I have table images of my restaurant. when I drag drop a particular reservation on a table, that table gets assigned to that reservation. When I click on tables image I show the table info in a popup. Now here comes the problem, to fetch table info page goes back to server(postback) and LINQ the collection and populate the info.This reduces the performance.What I want to do is to serialize tableInfo collection and save that XML in client's system and rather than postbacking the page I can fetch the table details from that XML through javascript. Is this possible?can I store that XML in clients machine?
OR what else I can do to increase performance??
It depends.
One way would be to use Web Storage (either localStorage or sessionStorage), but that requires a browser that supports it (very few do).
If you only need the data in the context of a single page (or don't have a problem with requesting it once per page), then you can simply keep it in some global variable (for example as a property of the window object).
There are a couple of options with this. First, you could continue to make the server side call but utilize jQuery/AJAX/loading images to make the wait less painful.
The other option would be to use HTML 5 local storage. Stephen Walther has a good example project of how to use it.

Categories