1
I have an HTML document with a form and I want to send a random key to the server. To do this, I’m using the element <keygen>
.
Before the data is sent, I need to get the value of this element <keygen>
to store the generated key locally. I tried the following code:
function getKey() {
const keygen = document.getElementsByTagName("keygen")[0];
sessionStorage.token = keygen.accessKey;
}
document.onsubmit = getKey;
The problem is that it always returns an empty string instead of the generated key. What should I do to get the key before sending the form?
<keygen>
is an obsolete feature of HTML5. I would question its use.– Luiz Felipe
I just want to make it clear that it was not I who gave the negative in the question. Although it is an obsolete element, I don’t think the question is bad... I’d like to know why the negative.
– Luiz Felipe
I also did not see the need for the negative.
– user181348
The attribute you were looking for is
challenge
but I’m sorry to inform you that the tag<keygen>
no longer works on any browser the API has been removed: Reference https://caniuse.com/#search=keygen– Augusto Vasques
So what should I use to generate random keys? I don’t want to create an entire function just for that ;/
– JeanExtreme002
@Jeanextreme002 This helps
Window.crypto
?– Augusto Vasques
Use <keygen> will return [Object Htmlunknownelement] in localStorage or sessionStorage.
– ElvisP
@Jeanextreme002, I think it’s worth [Edit] the question to tell you what you really need to do. If you want to generate random keys,
<keygen>
- deprecated tag -, does not seem to be a requirement, right?– Luiz Felipe