简体   繁体   中英

Pass data between two HTML pages (Google Apps Script)

I'm trying to pass var 'id' from the page 'a.html' to 'b.html'. The var content comes from 'code.gs' as below:

code.gs

function data(){
var id = 1;

return id;
}

Next, I get this var and I show it in 'a.html':

a.html

<?
var id = data();
?>

<h1><?= id ?></h1>

<a href="b.html">Go to B.html</a>

By clicking 'Go to B.html', the system directs the user to there. I need to bring the same value of var 'id' from the page 'a.html' to 'b.html'. Ps: searching for a little, I saw that there's a kind to send this var by the command 'localStorage', but it's not working for me. :(

Can anybody help me?

Use localstorage a.html

localStorage.setItem('id',1) 

b.html

var id  = localStorage.getItem('id')

the other way is to put it in a js file and import it in both html

Storing & Retrieving html data on the server

Client Side JavaScript:
<script>
function saveId(v) {
  google.script.run.saveKeyValue({key:'id',value:v});
}
function getId() {
  google.script.run
  .withSuccessHandler(function(v){
     alert('The value is ' + v );
   })
  .getKeyValue('id');
}
</script>

Server Side Google Apps Script:

function saveKeyValue(obj) {
  PropertiesService.getScriptProperties().setProperty(obj.key,obj.value);
}

function getKeyValue(key) {
  return PropertiesService.getScriptProperties().getProperty(key);
}

You could also replace PropertiesService with CacheService.

Client To Server Communications

Properties Service

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