Kaio Cardoso
Kaio Cardoso

Reputation: 5

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?

Upvotes: 0

Views: 1673

Answers (2)

Cooper
Cooper

Reputation: 64140

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

Upvotes: 1

Ioan Beilic
Ioan Beilic

Reputation: 473

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

Upvotes: 5

Related Questions