js在html页面之间传参

比如说有两个html页面,一个是a.html,一个是b.html;

一、地址栏传参

二、使用cookie

a.html的js中:

document.cookie = "key1="+value1;

document.cookie = "key2="+value2;

document.cookie = "key3="+value3;

location.href = "b.html";

b.htmld js中:

function getCookie(keys){

    var key = keys+“;”;

    var keysplit = document.cookie.split(';');

    for(var i = 0;i
var value1 = getCookie("key1");

var value2 = getCookie("key2");

var value3 = getCookie("key3");

三、使用localStorage或者sessionStorage

localStorage:

a.html:

localStorage.setItem("key1","value1");

localStorage.setItem("key2","value2");

localStorage.setItem("key3","value3");

location.href = "b.html";

b.html:

var value1 = localStorage.getItem("key1");

var value2 = localStorage.getItem("key2");

var value3 = localStorage.getItem("key3");

sessionStorage:

a.html:

sessionStorage.setItem('key1', JSON.stringify("value1"));

sessionStorage.setItem('key2', JSON.stringify("value2"));

sessionStorage.setItem('key3', JSON.stringify("value3"));

location.href = "b.html";

b.html:

var value1= JSON.parse(sessionStorage.getItem('key1'));

var value2 = JSON.parse(sessionStorage.getItem('key2'));

var value3 = JSON.parse(sessionStorage.getItem('key3'));



你可能感兴趣的:(前端)