How I can get UIWebViews id in java script? - javascript

I use UIWebView in the iOS project.
In js code in this webview I use local storage (reading data from local storage by key and writing data to local storage by key).
var items = JSON.parse(localStorage.getItem('key')) || []; //reading
localStorage.setItem('key', newJson); // writing
Now I use only one key for using local storage.
It works fine.
But local storage in common for all UIWebViews in the project (Am I right or not?). And when I use two or more UIWebView in the project (js code is similar for all webviews), that I need use more than one key for using local storage (one key for every UIWebView) in order to every webview use own storage.
How I can define which UIWebView uses local storage now in order to use key for this UIWebView?
How I can get UIWebViews id in java script?

You can save an object in localStorage().
In this way you can save data for more that one UIWebView in client memory and retrieve it easily by key.
An example:
// create an object with some properties (as value use data from UIWebView)
var uIWebViews = { 'uIWebView1': 1, 'uIWebView2': 2, 'uIWebView3': 3 };
// save your object into local storage
localStorage.setItem('uIWebViews', JSON.stringify(uIWebViews));
// retrieve the object from storage, remember to parse it
var retrievedObject = JSON.parse(localStorage.getItem('uIWebViews'));
// get the object
console.log('retrievedObject: ', uIWebViews);
// get a property, equivalent to get a key
console.log('key uIWebView2 has value: ', uIWebViews.uIWebView2);
Live example, please open console to see the result: https://jsfiddle.net/hsf8gf8d/

To let the JS code know what webview it runs in I would call its stringByEvaluatingJavaScriptFromString: method with a predefined JS method that sets a variable in your page. So your JS code could have something like
function setWebViewID(webViewID) {
// save the identifier somewhere and perhaps call other functions
}
Then in the webView, once the page is loaded and all, you call
[myWebView stringByEvaluatingJavaScriptFromString:#"setWebViewID(\"myID\")"]
The trick is usually ensuring that the content is done with any other stuff it does in JS right after loading. If you need something like that I'd recommend using WKWebView, WKUserContentController and to look into addScriptMessageHandler:name:. With that you can properly set up listening for events in JS from the webview.

Related

how to jump between pages with params while not changing the url

recently,i met with a problem that.
there is one page:pageA,there are some in it. the url of which will lead u to another page:pageB .
however ,i want to jump with some params or data while not changing the url of the pageB. is there any good solutions? i'm using react,but not a single page project.
There are couple of ways of doing it like using cookies, spring webflow(if its spring application ) or using Local/Session Storage
Web Storage is a feasible and an easy approach
In page A you can store the value in a local storage using key & value
if (typeof(Storage) !== "undefined") {
localStorage.setItem("anyunique Key Name ", "value which you want to store");
} else {
// Sorry! No Web Storage support..
}
In page B use same LocalStorage's getItem() method to retrieve the value
document.getElementById("someDOM").innerHTML = localStorage.getItem("key");

how to store javascript html dom document to locastorage

I have document (component tree) 'm' which i am trying to store in the local storage of html5. I tried setting it to local storage. but when i retrieve it back, this m has changed to [object Document].
How do i store document to local storage and retrieve it as document itself?
Below is the code i have tried
the application sends a ajax request to server to retireve events of a calendar and in the request's onsuccess the dom representation of xhtml is recieved.
when the user goes to next week view, the app has to retieve the event from the localstorage. to send the response to the client from the js file, the dom representation is also necessary.so we need to store the dom representation to the localstorage.
my requirements is in 1st picture's console, you can see 'k' is retrieved from the document m and is send as response to the client side.So, what I want is to store this k or m to local storage so i can manipulate it in the same js file.this code is in a separate js file which is used in the xhtml file.
I am using primefaces 4.0, jsf 2.1.
when i use
localStorage.setItem("calendarevents",JSON.stringify(k));
I get an error 'converting circular structure to json'.
You need to store a string representation of the document's html by doing:
localStorage.setItem('calendar', document.documentElement.innerHTML);
When you do this:
localStorage.setItem('calendar', document.documentElement);
...it stores the result of document.documentElement.toString() in localStorage, which doesn't work for your purpose.
localStorage converts every value to a string. To store objects you have to use a workaround.
I guess you could use the method described in this answer
var testObject = { 'one': 1, 'two': 2, 'three': 3 };
// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('retrievedObject: ', JSON.parse(retrievedObject));

Performance of edits to localStorage with JavaScript?

According to the W3 Web Storage specs, values in localStorage are of type string.
Thus, an entry can't be granularly updated like a subproperty of a JS object and it's only possible to replace the entire key:
Updating/editing localStorage - JSONObject
Assume I want to "secure" user input frequently on the client side in the localStorage, and also update it on model changes on the server (only transmitting changes from server to client). How often can I JSON.stringify() my local data (=ViewModel state) and save it to the localStorage without causing trouble for the user? Is serializing and saving (not transmitting!) e.g. 30KB of data every 5 seconds to the localStorage going to cause lags?
Bonus question: Does any major browser vendor plan on storing JS objects directly in localStorage?
This may not be entirely true; there is a method for updating a single key to an object housed in local storage, and the code is below.
var updateLocalStorageKey = function(obj, key, val) {
var localObj = JSON.parse(localStorage[obj] )
localObj[key] = val;
//reset storage
localStorage[obj] = JSON.stringify(localObj)
}
The working jsbin is here: http://jsbin.com/jesapifa/4/edit?html,js,output
Hope this solves your problem!

How to save a variable in a hta, so that after closing and opening the hta, it will be saved?

I am trying to make a hta (html application) where you can add names to an array, and then find out if a certain name is in the array. When I close and reopen the hta (or refresh for a html), none of the names are saved. How do I make it so that when I run the function for adding a name, the code saves with the variable having the name in it.
this is the function for adding a name
var names = []
function addName(first, last){
names.push(first + " " + last)
}
function realAddName(eventObject){
var addFirstName = document.getElementById("addFirstName")//the input box for the first name
var addLastName = document.getElementById("addLastName")//the input box for the last name
addName(addFirstName.value, addLastName.value)
alert("The name you input is now added to the thing.")
}
I don't have jQuery, so do not give me answers that use jQuery.
Please Help Me.
That's now how javascript works. When the browser is closed any values that you'ved added to the array do not get saved. This is the role of the database. jQuery wouldn't be able to accomplish this either.
Local storage maybe will help you. For example:
function addName(first, last) {
localStorage.setItem("usr123", first + " " + last)
}
You can add, change, or remove your data in local storage. But it's limited. You can find out more about localStorage here: HTML5 Web Storage
You need to persist your application's state to disk. Because this is a HTA it means you can use Scripting.FileSystemObject to interact with the local filesystem.
var fs = new ActiveXObject("Scripting.FileSystemObject");
var textFile = fs.CreateTextFile("foo.txt");
textFile.WriteLine(someVariable);
textFile.Close();
fs is an object that gives you access to the local filesystem: FileSystemObject
FileSystemObject has a method called CreateTextFile that creates a new textfile on disk and gives you an object (textFile) you can use to write text to this file.
WriteLine writes some text to the file
.Close() causes the textfile to be "saved" safely.
user2216996
If you really need to ask then you don't need a database for what you are doing. Try the "Hey, Scripting Guy!" solution at http://blogs.technet.com/b/heyscriptingguy/archive/2007/11/09/hey-scripting-guy-how-can-i-save-information-in-an-hta-as-a-tab-separated-values-file.aspx
If you really want to learn about databases then I suggest that you go to http://en.wikipedia.org/wiki/Database and start reading.

Local HTML 5 database usable in Mac Dashboard wigdets?

I'm trying to use HTML 5's local database feature on a Mac Dashboard widget.
I'm programming in Dashcode the following javascript:
if (window.openDatabase)
{
database = openDatabase("MyDB", "1.0", "Sample DB", 1000);
if (database)
{
...database code here...
}
}
Unfortunately the database-variable remains always null after the call to openDatabase-method. I'm starting to think that local databases are not supported in Widgets...
Any ideas?
/pom
No you will not be able to do the above. And even if you could then you would not be able to distribute the widget without distributing the database assuming it was a MySQL or SGLite. (not sure what you mean by HTML 5's local Db.
here are a number of ways round this:-
You can add a data source which can be a JSON file, or an XML file or and RSS feed. So to do this with JSON for example you would write a page on a server in PHP or something that accessed a database so that when the URL was called the result was a JSON string. Take the JSON string and parse it and use it in the Widget. This will let you get data but not save it.
Another way would be to use the user preferences. This allows you to save and retrieve data in the individual widget.
So
var preferenceKey = "key"; // replace with the key for a preference
var preferenceValue = "value"; // replace with a preference to save
// Preference code
widget.setPreferenceForKey(preferenceValue, preferenceKey);
You can then retrieve it with
var preferenceForKey = "key"; // replace with the key for a preference
// Preference code
preferenceForKey = widget.preferenceForKey(preferenceForKey);
The external call, you could also use REST will let you read any amount of data in and the preferences will let you save data for later reuse that will survive log out's and shut downs.
The Apple site has a lot of information about Widgets and tutorials as well thjat are worth working through.
Hope this helps.

Categories