Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中的不同html页面之间传递变量值_Javascript_Jquery_Html - Fatal编程技术网

如何在javascript中的不同html页面之间传递变量值

如何在javascript中的不同html页面之间传递变量值,javascript,jquery,html,Javascript,Jquery,Html,我想将所选列表项的值传递到另一个页面,这意味着如果我从列表中选择abc,则此abc值将传递到下一个html表单,它应该只打开该配置文件页面。是否有任何方法可以在不同的html页面中使用此变量 $('.ui-li-icon li').click(function() { var index = $(this).index(); text = $(this).text(); alert('Index is: ' + index + ' and text is ' + text

我想将所选列表项的值传递到另一个页面,这意味着如果我从列表中选择abc,则此abc值将传递到下一个html表单,它应该只打开该配置文件页面。是否有任何方法可以在不同的html页面中使用此变量

$('.ui-li-icon li').click(function() {
    var index = $(this).index();
    text = $(this).text();
    alert('Index is: ' + index + ' and text is ' + text);

我想把上面的文本值传递给我的profile.html,它有javascript函数profile()。所以我想在函数调用中传递这个文本,比如profile(text);我尝试在函数调用上方声明var文本,但仍然无效。请告诉我是否还有其他方法。

您可以使用
localStorage events
在网页之间传递值,如本演示所示:


有不同的方法可以做到这一点

将所选项目存储在cookies中

 // Store it in the cookies
 document.cookie="selected=john"

// Get it in the profile.html
var cookie = document.cookie;
将所选项目存储在本地存储器中

// Store it in the local storage
localStorage.setItem('selected', 'john');

// Get it from the local storage
var selected = localStorage.getItem('selected');
使用查询参数(推荐)


您可以在
profile.html?selected=john
的查询参数中传递所选项目。我推荐这种方法。您可以通过
位置读取所选项目。搜索

您可以将值作为url片段传递

在单击函数中,打开“/profile.html”+文本

在profile.html中获取url片段

示例代码:

导航到profile.html

window.location.href = '<path to profile.html>' + '#' + text;

HTML/HTTP是无状态的,这意味着,您在上一页上所做的/看到的与当前页完全断开连接

1) -使用前端存储非常简单,它可以配置任何浏览器(Cookie、会话存储、本地存储),并在一个页面中实现价值,在其他页面中实现价值

考虑到:

Cookie会保存数据,直到您确定的时间

会话存储将保存数据,直到关闭浏览器默认选项卡

本地存储保存数据,直到浏览器完全关闭并在选项卡(页面)之间共享此数据。它存储的数据没有过期日期,只能通过JavaScript清除,或者清除浏览器缓存/本地存储的数据-与cookie过期不同

2) –第二种方法是将其保存为请求参数-在通过Ajax渲染函数生成元素时向元素添加属性 链接 另一环节

->单击此元素构造“URL/?action=getAll&element=product&id=1212”后,在第二个页面中,即将退出的操作中,您可以解析此URL并使用适当的参数调用适当的Ajax

也许这些额外的信息也会有帮助

有关“客户端存储解决方案”的其他信息


在何处使用profile(text)?请查看另一个profile.html中我已包含此profile.js,在profile.js中它包含函数profile(text){};您还可以使用
document.referer
获取查询参数。
// Store it in the local storage
localStorage.setItem('selected', 'john');

// Get it from the local storage
var selected = localStorage.getItem('selected');
window.location.href = '<path to profile.html>' + '#' + text;
var text = window.location.hash.substring(1)