2
hello, I’m passing a jQuery code that I was developing, for Typescript... I had made in Jquery a withdrawal of cookies that was working perfectly.
What I need is to pass to the avriável, the value of a specific cookie, in the same way that is shown in the code that is in jQuery.
Anyway, I’ll leave the code so you can help me.
jQuery:
var numeroSerie = $.cookie('ns');
var user = $.cookie('user');
$('#numeroSerie').val(numeroSerie);
$('#user').val(user);
Typescript:
var numeroSerie = document.cookie;
var user = document.cookie;
(<HTMLInputElement>document.getElementById('numeroSerie')).value = numeroSerie;
(<HTMLInputElement>document.getElementById('user')).value = user;
When I get back what’s in document.cookie
, I get the following reply:
Undefined=true; Remember=true; rempass=true; rem_pass=true; pass=asdexpires=7; check_rem_pass=trueexpires=7; remember_pass=trueexpires=7; ns=Asd; user=Asd; check_rem_login=null; remember_login=null; remember_pass
Cookies are stored as a single string, what jQuery does is parse that string to retrieve the value of each key. As JS, and by extension TS, do not have a native cookie parse, you will have to create yours, or use some already ready, but your question does not make your goal clear. Do you want to turn this code into TS because you can’t use jQuery, or just because you want TS code? Because if that’s the case, you can download
@types/jquery
to get jQuery with TS support.– Andre
I am transforming all my jQuery code to TS, as it was a request from the company. So I will stop using jQuery and pass everything to TS. @user140828
– user149429
jQuery and TS are not mutually exclusive. TS is a superset of JS, you will still use libraries and frameworks with TS. You can use TS with jQuery, Lodash, Moment, React, Vue, none of which makes your code less TS. The question is, do you need this parse without jQuery? Why you can have this code on jQuery and TS at the same time.
– Andre
@user140828, so dude, thanks to your first answer, I talked here, and I was told I could use jQuery with TS, but it’s not ideal you know? If there was a way I wouldn’t use it, it would be better...
– user149429