How to pass data from one HTML page to another HTML page using JQuery?

I have two HTML pages that work in a parent-child relationship in this way:

The first one has a button which does two things: First it requests data from the database via an AJAX call. Second it directs the user to the next page with the requested data, which will be handled by JavaScript to populate the second page.

I can already obtain the data via an ajax call and put it in a JSON array:

$.ajax({
    type: "POST",
    url: get_data_from_database_url,
    async:false,
    data: params,
    success: function(json)
    {
        json_send_my_data(json);
    }
});

function json_send_my_data(json)
{
    //pass the json object to the other page and load it
}

I assume that on the second page, a "document ready" JavaScript function can easily handle the capture of the passed JSON object with all the data. The best way to test that it works is for me to use alert("My data: " + json.my_data.first_name); within the document ready function to see if the JSON object has been properly passed.

I simply don't know a trusted true way to do this. I have read the forums and I know the basics of using window.location.url to load the second page, but passing the data is another story altogether.

Answers


Warning: This will only work for single-page-templates, where each pseudo-page has it's own HTML document.

You can pass data between pages by using the $.mobile.changePage() function manually instead of letting jQuery Mobile call it for your links:

$(document).delegate('.ui-page', 'pageinit', function () {
    $(this).find('a').bind('click', function () {
        $.mobile.changePage(this.href, {
            reloadPage : true,
            type       : 'post',
            data       : { myKey : 'myVal' }
        });
        return false;
    });
});

Here is the documentation for this: http://jquerymobile.com/demos/1.1.1/docs/api/methods.html

You can simply store your data in a variable for the next page as well. This is possible because jQuery Mobile pages exist in the same DOM since they are brought into the DOM via AJAX. Here is an answer I posted about this not too long ago: jQuery Moblie: passing parameters and dynamically load the content of a page


session cookie may solve your problem.

On the second page you can print directly within the cookies with Server-Script tag or site document.cookie

And in the following section converting Cookies in Json again

How about?


Disclaimer: This is terrible, but here goes:

First, you will need this function (I coded this a while back). Details here: http://refactor.blog.com/2012/07/13/porting-javas-getparametermap-functionality-to-pure-javascript/

It converts request parameters to a json representation.

function getParameterMap () {
    if (window.location.href.indexOf('?') === (-1)) {
        return {};
    }
    var qparts = window.location.href.split('?')[1].split('&'),
        qmap   = {};
    qparts.map(function (part) {
        var kvPair = part.split('='),
            key    = decodeURIComponent(kvPair[0]),
            value  = kvPair[1];

        //handle params that lack a value: e.g. &delayed=
        qmap[key] = (!value) ? '' : decodeURIComponent(value);
    });
    return qmap;
}

Next, inside your success handler function:

success: function(json) {
    //please really convert the server response to a json
    //I don't see you instructing jQuery to do that yet!
    //handleAs: 'json'

    var qstring = '?';
    for(key in json) {
        qstring += '&' + key + '=' + json[key];
        qstring = qstring.substr(1); //removing the first redundant &
    }
    var urlTarget = 'abc.html';
    var urlTargetWithParams = urlTarget + qstring;

    //will go to abc.html?key1=value1&key2=value2&key2=value2...
    window.location.href = urlTargetWithParams;
}

On the next page, call getParameterMap.

var jsonRebuilt = getParameterMap();
//use jsonRebuilt 

Hope this helps (some extra statements are there to make things very obvious). (And remember, this is most likely a wrong way of doing it, as people have pointed out).


Here is my post about communicating between two html pages, it is pure javascript and it uses cookies: Javascript communication between browser tabs/windows

you could reuse the code there to send messages from one page to another.

The code uses polling to get the data, you could set the polling time for your needs.


You have two options I think.

1) Use cookies - But they have size limitations.

2) Use HTML5 web storage.

The next most secure, reliable and feasible way is to use server side code.


Need Your Help

KendoUI. KendoDataSource not loading data from webservice

javascript json html5 kendo-ui

I have been trying to get to get my kendoUI project up and running for days now and now I am stuck. First I had troubles with the webserver that I created to return in json format but I managed to ...

How to create Google Latitude like markers?

javascript jquery google-maps google-maps-markers

In my HTML5 app, I use Google Map v3 and add several markers on a map. It's easy to place new markers and to change icons but I'd like to be able to build markers like the one used in Google Latitu...