Transfer data from one HTML file to another

Try this code:
In testing.html

function testJS() {
    var b = document.getElementById('name').value,
        url="http://path_to_your_html_files/next.html?name=" + encodeURIComponent(b);

    document.location.href = url;
}

And in next.html:

window.onload = function () {
    var url = document.location.href,
        params = url.split('?')[1].split('&'),
        data = {}, tmp;
    for (var i = 0, l = params.length; i < l; i++) {
         tmp = params[i].split('=');
         data[tmp[0]] = tmp[1];
    }
    document.getElementById('here').innerHTML = data.name;
}

Description: javascript can’t share data between different pages, and we must to use some solutions, e.g. URL get params (in my code i used this way), cookies, localStorage, etc.
Store the name parameter in URL (?name=…) and in next.html parse URL and get all params from prev page.

PS. i’m an non-native english speaker, will you please correct my message, if necessary

Leave a Comment