How to pass localstorage variable to different angularjs project - javascript

I am new to angularjs. I want to pass localstorage variable from one angularjs project to another angularjs project. Both projects are on same server.
I am passing localstorage variable from http://site1.in/ as,
window.localStorage.setItem('is_provider', 1);
and I want to use this localstorage variable to http://site2.in/.
I don't know how to get this variable in site2.
Please help.

You can't, localStorage is per domain specific.

Local Storage Privacy
Website A and Website B would have their own local storage. Usually you would have to store certain information in a server database and sync it to the local storage.
I would use the local storage as a cache to get data once and update it at a certain interval depending on when I would want to invalidate the cache. For instance, you could sync with the server when the user A would log out and user B would want to login.
Have a look at the Privacy section in the HTML5 spec for Web Storage.
More information information and resources here: HTML5 Rocks.
Testing
I would suggest the use of a local server setup such as Linux/Mac/Windows, Apache, MySQL, PHP stack (LAMP/MAMP/WAMP) to test on localhost (127.0.0.1).
Most browsers will limit you to 5 MB per domain for every window and tab because of the HTML5 spec recommendation.
I haven't tried this, but you could perhaps have a look at changing the port number of the localhost in Apache's httpd.conf (to do so, find Listen and change the port associated to it) and see if this will do the trick. Basically, you run each test under a different port number to have the whole storage limit for each test.
An alternative would be to create a Chrome extension. You can read more information about this here:
Managing HTML5 Offline Storage
Manifest files
Maybe this link can help: http://www.nczonline.net/blog/2010/09/07/learning-from-xauth-cross-domain-localstorage/
And NO they cannot be without trickery

I found a way to pass data from one domain to another. You can use cross domain local storage to send data. I have used this and it's very simple. Refer to the site https://github.com/ofirdagan/cross-domain-local-storage

Related

SQLite database with javascript

I have a requirement where I have a postgresql database in a web site.
I want to run my web site in offline mode but the problem is that I have many ajax calls in my website which will not work in offline mode.
So I am considering using sqlLite but I don't know how to configure it, how to write JavaScript code, or even know if the users need to install sqlite in their browser or PC. Can anyone help to overcome this requirement?
I have used some local storage like Indexed DB it will work but that is called sqlLite or not I don't know.
please help
You do not need to work with Sqlite for addressing this, only take a look at following link for how to make web pages available for offline viewing.
If you namely want to use some database it is possible to use SQLite.
Look at https://github.com/kripken/sql.js/
Be care of using SQLite requests in main UI thread. Do not forget to implement workers for SQLite.
I'm pretty sure that you do not need SQLite.
Try using HTML5 LocalStorage API.
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
The Storage interface of the Web Storage API provides access to the session storage or local storage for a particular domain, allowing you to for example add, modify or delete stored data items.
If you want to manipulate the session storage for a domain, you call Window.sessionStorage method; If you want to manipulate the local storage for a domain, you call Window.localStorage.
https://developer.mozilla.org/en-US/docs/Web/API/Storage

How to save html, javascript and css files in html5 local storage?

Is there a way to store a html, javascript and css files in html5 local storage?
I want make my webapplication faster!
Thanks
You want to use the Application Cache for this, not localStorage:
http://www.html5rocks.com/en/tutorials/appcache/beginner/
You don't use HTML5 Local storage for CSS/JS files in, you cache them.
I suggest to use cache on client and server side instead of storing in local storage this way you have control on when to update(invalidate cache), you can also consider using CDN to deliver static content like images, js, css
Even though it's not the best solution, it is possible to cache most of the interface with local storage. Christian Heilmann discussed a lot of ideas in his 2010 24Ways article. Have a look at this screencast for a full example along the lines you're thinking.
Note that to cache images in local storage you will need to Base 64 encode them first.
Local storage is a key value store you should prefer to store data objects (json) or single values like strings ore numbers. Don't save the representation layer inside.
You can absolutely use it.
When you configure your cache fot the browser to checks if a file has changed or not it will request a file and the CDN will respond with 304 status code which means the file in the browser cache can be used. However this still requires an HTTP request. You can also use etag or expires so it doesn't do that.
However it could happen that you load external files from sites where you have no control over the header. In that case to not have the http request and 304 response you could save time by using local storage.

How can i get the HTML5 Local storage values in server side

I am a .Net developer, I know that the HTM5 localstorage is client-side storage technique. I want to get the local storage data on the server-side.
For getting cookie value from server-side we have Request.Cookie in ASP.NET. Is there any solution like that to take the local storage value directly on the server-side? Please guide me. I am using the .net 4.0 framework
Thanks,
Jibu
You will need to pass this information from the client to the server using standard HTTP techniques. Using javascript you could fill:
Hidden fields
Query string parameters
POST
Ajax call to the server
...
It will all depend on how your application is organized, what kind of information is being stored, its volume, whether you want to redirect or not, ... But in all cases this should be done using javascript since that's the only way to access data stored in localStorage.
No. The whole point of local storage is that it is local. One of the advantages of it over cookies is that you can store lots of data in it. One of the advantages of cookies is that they are tiny so the overhead of including them in every HTTP request to a given host is small. There two advantages are incompatible so you won't want them in a single technology.
If you want to get the data on the server, then you need to get the client to send it explicitly (e.g. via Ajax).
This is a widescope question. (like the length of a piece of string), but Ill try to make this helpful:
If you have values in local store in webserver I assume your webserver is JSON? Or did you use the sql local storage option?
Regardless of type of storage, you need to build an interface that both handles:
a) Reading data from your local database -> its important to involve some kind of date or index value in here if you are aiming to sync databases... this is to make sure you send IN ORDER all transactions / updates which are in your database. For this to happen you must store your data not only as tables with inforamtion but also tables that contain events of when updates happened and what was updated. (change tables). This will help check in the server end that everything is sync and also means you dont send data to the server that is not needed and can be kept locally. ((otherwise what is the point of local store if you cant save yourself server database space by only syncing waht is necessary?)
b) A HTTP local server to send the data to your destination client server or database server, etc (however you have set your infrastructure) - I recommend using industry standards for your language and server, which is Ajax and JQuery. If you do a lot of streaming of data then i recommend looking into RXjs with Ajax to get a http interface built (interface in this sense just means a way to expose your client like an API and post http calls)
c) An event loop to handle how often and what triggers the synchronization so that you dont destroy your users machine with overdoing it (you dont want to do this too often, but also want to it to be meaninful rather than "every night" maybe user enabled whenever you detect an event which triggers wifi available again.) - i recommend using native wifi reading capabilities built into Apache Cordova and also industry standards for your server setup (for example Express.js for Node.JS).
Obviously the backend server needs to have its API set up and authentication / authorizations, etc.

How can JavaScript get data from a user's computer?

How can Javascript (running from a website on a remote server) access data from a user's local computer?
I have access to all the user's computers and they are all using Chrome.
The data may be stored in a file, a database, I can even run a webserver on their computers if needed, etc.
Is there any way for my users to for example allow my website to access data from JavaScript?
(maybe be there is a Chrome setting but I still want their browser to be secure)
As far as I know, you cannot access the files on the user's machine using javascript. This would be a huge security hole. You have to write a client (not web) application (windows app or whatever technology you are using).
this is not possible just using a browser, because as you said yourself it would`t be secure. You can use some kind of plugin which your users have to allow which then can access the local data on the computer.
It sounds like you are flexible as to where the data is stored in the user's computer. In that case use localStorage or Web SQL Databases. Web SQL Databases are no longer part of the HTML5 spec, but they are supported by Chrome. Using either of these methods will store the data on the users computer - right in the browser in fact.
Edit: In the application that provides the caller id data, have it launch your website with chrome passing in a querystring argument containing the needed caller id data. Then in your page, have it listen to the onstorage event, and update the dropdown when the appropriate localStorage data has changed.
In your caller id app, call:
%ChromeInstallPath%\chrome.exe "http://foo.com/caller?Bill+Gates#425-882-8080"
In your caller handler, use this JavaScript:
window.onload = function() {
localStorage.callerId = location.search;
};
In your page with the drop down list, use this JavaScript:
window.onstorage = function() {
setDropDownFromCallerId(localStorage.callerId);
};
What do you mean by access? does the server-side code need to grab additional info during processing a request?
Is this data that is exclusive to your app (that is, only you create, read, update it.)
If this data is exclusive to your app, then why not store it in HTML 5 Local Storage

Are cookies the only native cross-subdomain storage?

Cookies are great because a value written in website.com can be used in www.website.com (www is considered a sudomain of no-www). The downside is all the cookie values are sent along with every HTTP request to the server. So I'm looking for a local storage mechanism available natively to Javascript that works cross-subdomain and isn't transmitted to the server. Does such a mechanism exist? LocalStorage doesn't work cross-subdomain and Flash Cookies wouldn't work on iPhone.
Perhaps just redirect website.com to www.website.com or vice versa?
This seems like it would be the simplest fix.
http://www.scriptalicious.com/blog/2009/04/redirecting-www-to-non-www-using-htaccess/
If your users have an actual account that they login to on your server, then you could store the info server-side and just include a little javascript in the each page that will need that data with the appropriate data. When you render the page server-side, you can define a user object in javascript with appropriate attributes set to the data values that can then be referenced client-side. That way, you only include the data that is needed in a given page, the same user data is available no matter what computer the user logs in from (no reliance on persistent cookies). If larger pieces of data are needed only occasionally and you don't want to include them in the page in case they are needed, then make those pieces of data queryable via ajax/json so they can be retrieved only when needed.
If you're still intent on only storing it locally, then cookies or HTML5 local storage are your only options and cookies will be your only cross browser option that covers all browsers in use. At the addition of implementation complexity, you could combine a number of the suggestions:
Always redirect to www.domain.com so all user activity is on the same domain.
Use HTML5 local storage when available (the redirect in step 1 prevents sub-domain lockout).
Fall back to cookie storage when HTML5 local storage is not available.
One could presumably write or find an abstraction for HTML5 local storage and cookies so 99% of your code could be independent of which storage mechanism was actually being used. It looks like there are some jQuery plugins that do exactly that.

Categories