简体   繁体   English

我可以从两个页面将变量发送到相同的JavaScript吗?

[英]Can I send variables to the same JavaScript from two pages?

I have 2 pages where the user gives data that needs to be stored (the 1st is a string from the first page and the rest comes from a form from the second page). 我有2个页面,用户在其中提供需要存储的数据(第一个是第一页中的字符串,其余的是第二页中的表单)。 This webpage is not on a server, its on a laptop without any connection to the internet. 此网页不在服务器上,而是在笔记本电脑上,没有与互联网的任何连接。 The plan is for all that data to be written on to a text document. 该计划是将所有这些数据写入文本文档。

My question is; 我的问题是; can I send the first variable to a JavaScript like var = get.element... and then redirect the user to the second page where more variables get sent to the same JavaScript without the first disappearing. 我可以将第一个变量发送到var = get.element...这样的JavaScript,然后将用户重定向到第二个页面,在该页面上,更多变量被发送到同一JavaScript,而第一个不消失。 Because from what I have understood, all the scripts and variables "reset" when reloaded. 因为据我了解,所有脚本和变量在重新加载时都会“重置”。

Extra, this is how I plan to write the info in a text file, does it look good or is there a better way to store data locally? 另外,这是我计划将信息写入文本文件的方式,看起来不错还是在本地存储数据的更好方法?

<?php
function createFile(){
    $file = 'D:\test.txt';
    // Open the file to get existing content
    $current = file_get_contents($file);
    // Append a new person to the file
    $current .= "John Smith\n";
    // Write the contents back to the file
    file_put_contents($file, $current);
}
?>

Thanks for all your time! 感谢您的宝贵时间!

I would think to pass some parameters from the first page using GET or POST request to the second page. 我想将一些参数使用GETPOST请求从第一页传递到第二页。

For example: 例如:

//yourApp/second_page.php?name=John Smith

is requested from the first page so you can get the data in the second page using $_GET['fname'] 是从第一页请求的,因此您可以使用$_GET['fname']在第二页中获取数据

The solution @shadow proposed (using html5 local storage api) seems more clean and simple tho. @shadow提出的解决方案(使用html5本地存储api)似乎更简洁明了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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