简体   繁体   中英

jQuery passing data between pages

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);
    });
});

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);
});

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.

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)
}

You could try cookies if it is on the same domain.

You'll need to use the jQuery cookie plugin (to iron out cross browser issues).

You should try to do something like this:

  1. Generate the variable on page 1.
  2. Save this variable as a session cookie.
  3. On page 2, you can now access the session cookie.
  4. If the user directly visited page 2 without visiting page 1 you should have a default value setup.
  5. Done!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM