Recover cookie with Typescript

Asked

Viewed 193 times

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

  • 1

    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.

  • 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

  • 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.

  • @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...

1 answer

0


If what you need is a cookie parse without jQuery, you can use the function suggested by w3schools

function getCookie(cname: string): string {
    const name = cname + '=';
    const decodedCookie = decodeURIComponent(document.cookie);
    const ca = decodedCookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return '';
}

Just invoke it by passing the cookie name

const ns = getCookie('ns');
const user = getCookie('user');
  • Thank you, it really worked for these two, but I left only these two as an example. If I want to take more data from these cookies, using this function is just me doing the same way you did with the ns and the user? Or it is necessary to change something in the function?

  • You can leave, I managed to make everything work here, thanks. It worked perfectly and very simply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.