0

Is url hash is the only way to provide data into browser page?

For example i want to save some user settings without server-side and retreive them from user on another pc.

With hash it's possible to generate an url and ask user to save the url or shortcut on USB Flash.

If there any other way to do that?

Somebody
  • 9,316
  • 26
  • 94
  • 142
  • Maybe you should explain why you want to do such thing.. It makes no sense :\ If you want browser portability, settings should be saved in the server.. – tiagoboldt Mar 22 '11 at 10:43
  • I want to avoid user registration. – Somebody Mar 22 '11 at 10:50
  • Well, there's the query string `?user=foo&hobby=bar`... Otherwise you need server side user registration (portable) or cookies (not portable) – BGerrissen Mar 22 '11 at 11:00
  • Use openid.. login with twitter, facebook and google and it's almost sure that the user will have one of them! – tiagoboldt Mar 22 '11 at 11:10

2 Answers2

0

You say it: with hash it's possible to generate an url. So the url has to be unique in way to provide data to the browser page. There really is no other way client-side.

www.data-blogger.com
  • 4,076
  • 7
  • 43
  • 65
  • It's possible to save a link with hash. Later user just follow that link and javascript reads hash from it. – Somebody Mar 22 '11 at 10:56
0

As your URL is the only way to make resources unique (or: save settings), you can use anything it it to store data. Note that only the hash part is not sent to the server.

location properties for the URL http://www.iana.org/domains/example/page?data=xyz#hash:

{
    "hash": "#hash",
    "host": "www.iana.org",
    "hostname": "www.iana.org",
    "href": "http://www.iana.org/domains/example/page?data=xyz#hash",
    "pathname": "/domains/example/page",
    "port": "",
    "protocol": "http:",
    "search": "?data=xyz"
}

It's not recommended to transfer sensitive data (password, personal information) in this way. Even if it's not visible on the server, it will be visible in the browser history.

You can use the encodeURIComponent function to escape special characters like # or &.

Example for hash URLs like #field:data;field2:data2 (other separators could be used as well):

// default settings
var settings = {
   "lang": "en",
   "style": "green" 
};
// this should be called before the settings are used
function loadSettings() {
    // get the part after # and split it by `;`
    var hashSettingsArray = location.hash.substr(1).split(";");
    for (var i=0; i<hashSettingsArray.length; i++) {
        var hashSettingArray = hashSettingsArray[i].split(':');
        var key = decodeURIComponent(hashSettingArray[0]);
        // if the key exists in the settings object, accept it
        if (key in settings) {
            // accept #field without value, defaulting to an empty string
            var value = hashSettingsArray.length > 1 ? decodeURIComponent(hashSettingArray[1]) : "";
            settings[key] = value;
        }
    }
}
function getSettingsHash() {
    var settingsArray = [];
    for (var setting in settings) {
        var key = encodeURIComponent(setting);
        var value = encodeURIComponent(settings[setting]);
        settingsArray.push(key + ":" + value);
    }
    // example return value: #lang:en;style:green
    return "#" + settingsArray.join(";");
}
Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
  • Btw how much data is possible to store in one hash? Is it possible to make a chain of hashes to be opened in a url somehow? So that they could be concatenated and stored in localStorage or cookie. – Somebody Mar 22 '11 at 11:29
  • For the maximum length of an URL, see the answers on [max length of url hash parameter](http://stackoverflow.com/q/1571753/427545). It's not a good idea to use mulitple URL's, why don't you create a textfield with all settings stored in it, so the user can copy it over? – Lekensteyn Mar 22 '11 at 11:34
  • Yea, but user should open a notepad, copy them to notepad, then save them into USB device. But with url shortcut all is easier. Just drag url to usb folder and double click it on another PC. :) Isn't it's easier? – Somebody Mar 22 '11 at 11:37
  • Why don't you make a HTML form which the user can save in notepad? Then the user should just open the `saved.html` file and the browser will import the data. – Lekensteyn Mar 22 '11 at 11:43
  • So is there a way to create a chain of urls to be opened in the same tab? Some cross-browser parameters in shortcut maybe? – Somebody Mar 22 '11 at 11:43
  • If you're not afraid of using new technology, you could use the postMessage API for cross-site data transfer. Otherwise, you could change the location hash dynamically. (by using frames for example). Note, you have to change **location.hash**, not **location.href**, otherwise the page just navigates away. You need some way to acknowledge receipt of the hash. – Lekensteyn Mar 22 '11 at 13:10