how to pass json value from one page to another (Help needed)

889 Views Asked by At

using this script to parse the json value and display on first page (home.html)

I am using href tag to go on second page(Details.html)

$(function() {
    var RETAIL_CRM = [];
    var dmJSON = "https://cordova.eu-gb.mybluemix.net/CRM.jsp";
    $.getJSON(dmJSON, function(data) {
        $.each(data.RETAIL_CRM, function(i, f) {

I just want to send these three (f.S_NO , f.NAME and f.STATUS) values from home.html to Details.html

var tblRow = "<tr>" + "<td>" + "<a href=Details.html?localStorage.getItem=" + f.S_NO + "&value1=" + f.STATUS + "&value2=" + f.NAME + ">" + f.S_NO + "</td>" + "</a>" + "<td>" + f.NAME + "</td>" + "<td>" + f.STATUS + "</td>" + "</tr>";
            $(tblRow).appendTo("#list");
        });

These input types are on the second page (Details.html) i just want to set valued in it .

document.getElementById("crm_serialnumber").value = localStorage.getItem;
        document.getElementById("crm_name").value = value1;
        document.getElementById("crm_status").value = value2
    });
});

I need some help .Please .

1

There are 1 best solutions below

1
imixtron On

firstly i would recommend using a JavaScript framework rather than plain HTML as it makes life much more easier (look into angular/react).

the easiest way for this is to use native localStorage function, which is supported in many browsers, but you are using it the wrong way.

usage as stated by documentation:

localStorage.setItem('key','value')
var key_val = localStorage.getItem('key')

you can also use the following snippet that i made for my usage:

var storage = {
    get: function (key, skipParse) {
        var data = localStorage.getItem(APP_PREFIX + key);

        if (data) {
            if (!skipParse) {
                data = JSON.parse(data);
            }

            return data;
        }
    },
    set: function (key, value, skipParse) {
        if (!skipParse) {
            value = JSON.stringify(value);
        }

        localStorage.setItem(APP_PREFIX + key, value);
    },
    remove: function (key) {
        localStorage.removeItem(APP_PREFIX + key);
    }
}

look at my gist for complete details: $helper


UPDATED if you want to get query parameters, use the following code on the Details.html page:

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

// Usage will be like this
getParameterByName("value1") // will return f.STATUS in your case.
getParameterByName("value2") // f.NAME.. vice versa..

getting query parameters using javaScript