Get a value in the UI from the index.html

Hi! I’m working with Vaadin 16 and store the value of a token into a variable in the index.html
How can I get the value of it in the UI?
I try to get it with PendingJavaScriptResult result = UI.getCurrent().getPage().executeJs(“document.getElementById("id_token").value”); but i don’t know how to get the real value

Hi Lisa

In general, writing a token into html sounds a little sketchy. I don’t know the big picture here so I’ll just answer your question about how to get the value into the Java code.

If you return something in the executed js, it lets you use that value (as JsonValue) after it has completed.

UI.getCurrent().getPage().executeJs("return document.getElementById('id_token').value;").then(jsonValue -> {
	String idToken = jsonValue.asString();
	// do stuff with the idToken
});

Kaspar Scherrer:
Hi Lisa

In general, writing a token into html sounds a little sketchy. I don’t know the big picture here so I’ll just answer your question about how to get the value into the Java code.

If you return something in the executed js, it lets you use that value (as JsonValue) after it has completed.

UI.getCurrent().getPage().executeJs("return document.getElementById('id_token').value;").then(jsonValue -> {
	String idToken = jsonValue.asString();
	// do stuff with the idToken
});

Hi!

Thaks for the help!

I’m trying to get the Google SignIn token that I keep in the index.html, but the executeJs() you told me doesn´t get nothing.

I’m trying this way because with the OAuth 2 and Google Sign In tutorial for Vaadin 14 (https://vaadin.com/learn/tutorials/google-login) I have not succedded.