If the two pages are on the same domain, a third way is to use HTML5 localStorage: http://diveintohtml5.info/storage.html
In fact localStorage is precisely intended for what you want. Dealing with GET params or window/document JS references is not very portable (even if, I know, all browsers do not support localStorage).
Answer from Ulflander on Stack OverflowIf the two pages are on the same domain, a third way is to use HTML5 localStorage: http://diveintohtml5.info/storage.html
In fact localStorage is precisely intended for what you want. Dealing with GET params or window/document JS references is not very portable (even if, I know, all browsers do not support localStorage).
Here's some very simple pure JavaScript (no HTML, no jQuery) that converts an object to JSON and submits it to another page:
/*
submit JSON as 'post' to a new page
Parameters:
path (URL) path to the new page
data (obj) object to be converted to JSON and passed
postName (str) name of the POST parameter to send the JSON
*/
function submitJSON( path, data, postName ) {
// convert data to JSON
var dataJSON = JSON.stringify(data);
// create the form
var form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', path);
// create hidden input containing JSON and add to form
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", postName);
hiddenField.setAttribute("value", dataJSON);
form.appendChild(hiddenField);
// add form to body and submit
document.body.appendChild(form);
form.submit();
}
Use some PHP like this on the target page to get the JSON:
$postVarsJSON = $_POST['myPostName'];
$postVars = json_decode( $postVarsJSON );
Or, more simply for JavaScript:
var postVars = JSON.parse( <?php $_POST['myPostName']; ?> );
How do I pass a portion of Json object to another page?
How do you pass json data from one page to another to reference the same row of data?
php - how to pass json data to page to page? - Stack Overflow
how to pass json data into query string and use that data in another page
Videos
You can dynamically create a hidden form with inputs containing your data inside, and submit it to desired address
Example using jQuery:
var url = 'index.html';
var data = {
a: 1,
b: { a: 1, b: 2},
c: [1, 2, 3]
};
function appendData($form, prefix, data) {
function append(k, data) {
if (prefix !== '') {
appendData($form, prefix + '[' + k + ']', data[k]);
} else {
appendData($form, k, data[k]);
}
}
if (data instanceof Object) {
if ($.isArray(data)) {
for (var i = 0, l = data.length; i < l; i++) {
append(i, data);
}
} else {
for (var k in data) {
append(k, data);
}
}
} else {
var inp = $('<input type="hidden" name="' + prefix + '">').val(data);
$form.append(inp);
}
}
var $form = $('<form style="display:none;" action="' + url + '" method="post"/>');
appendData($form, '', data);
$('body').append($form);
$form[0].submit();
http://plnkr.co/edit/bM4oAj7TZNWfjkLVWMrY
If you want to change the phyiscal page in the browser, then you have to get the browser to do it. You can have an AJAX in the background, but it's not going to affect the browser page.
The simpliest way to get the browser to change the page is to set the window.location property to the url of the new page. Note, however, you can only do a GET this way.
I'd suggest doing an AJAX POST, and have that return a URL which you can have the borowser get to display the page you want.
EDIT
var options = {foo:'foo'};
var myURL="http://localhost";
window.open( myURL + "/?options=" + JSON.stringify(options) );
didn't test that code before, try this, you can access it through GET
I see there are several ways to do this:
- Cookies. Write values to cookies and read it in the opened window.
- Pass as a part of url hash: window.open(url + '#' + encodeURIComponent(JSON.stringify(json));
I would try to
winRef = window.open(...);
winRef.postMessage(...);
https://developer.mozilla.org/en/DOM/window.postMessage
I didn't try the third option, but it might be a nice alternative to 1 and 2.