Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery passing data between pages

Tags:

jquery

I am new to jQuery. Is there any way to retrieve the value of booked in another page through jQuery?

$(document).ready(function() {
    $(".active").click(function() {
        var booked=$(this).val();
        confirm(booked);
    });
});
like image 813
user1589936 Avatar asked Sep 01 '12 09:09

user1589936


3 Answers

Use cookies or HTML5 localStorage if its purely on the client-side.

localStorage.setItem('bookedStatus' + customerId, true);

Else use ajax if the data has already been submitted to server.

$.get('/site/getBookingStatus?customerId=' + customerId, function(data){
   alert(data);
});
like image 158
Robin Maben Avatar answered Sep 27 '22 17:09

Robin Maben


Alternatively, if this is a simple string you can append with the URL of the page while navigating to another page. If this is secured data, you can encrypt the string and attach.

Your URL will be : example.com/nextPage?x=booked

In the next page you can get the string by decoding it as given :

var encodedData = window.location.href.split('=')[1];
var bookedValue = decodeURI(encodedData);

If you have encrypted the script, you have to decrypt in the next page.

like image 33
ruveena Avatar answered Sep 27 '22 17:09

ruveena


localStorage doesn't work well on mobile browsers. I've given up on trying to get localStorage to work on iPhone/safari. If you're not passing too much data then a simple solution is to attach the data to the url you are navigating to, using the usual ?param= syntax:

// set your data in the source page 
function set_url_data(go_to_url, data) {
  new_url = go_to_url + '?data=' + data;
  window.location.href = new_url;
}

// parse your data in the destination page 
function grab_data_from_url() {
  url = window.location.href;
  data = url.split('data=').pop();
  return(data)
}
like image 28
Cybernetic Avatar answered Sep 27 '22 17:09

Cybernetic