简体   繁体   中英

How to pass a variable ( a numerical ID) from a page to another through click

I have auto generated content, and auto generated buttons. The buttons have the same link, only the variable differs. How can i pass the variable on the button click to another page ( from the button onclick link).

After I generate the button, i give it a function, something like:

$('.Button').click(function() { window.location ='page.html'; }); 

You could simply use a query string.

http://mysite.com/page2.html?myid=1234

Based on your comment you could do:

$('.Button').click(function() { 
     var myid = 1234;
     window.location ='page.html?myid=' + myid; 
});

If you want to do it using pure html/js - try to add hash to page like in code above:

onClick {
 document.location = "/yourPage.html#id";
}

onLoad {
var id = window.location.hash;
}

Also, you can save id in SessionStorage, LocalStorage or Cookies.

If you don't care about the variable being public you can add it to the url as a query string http://domain/pagename.html?varname=2&othervar=2 .

If its on the same domain you can add it to sessionStorage

sessionStorage["varname"] = x;

You could set it as a variable in the URL query string, and then read the query string for the value.

Something like:

index.html?variable=value

Another option would be to set a cookie with the variable key/value, and then read the cookie when the page loads.

document.cookie=variable + "=" + value;

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