简体   繁体   中英

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

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']

The solution @shadow proposed (using html5 local storage api) seems more clean and simple tho.

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