Skip to content

Get cookies #99

Answered by wang0618
mawoka-myblock asked this question in Q&A
May 15, 2021 · 1 comments · 18 replies
Discussion options

You must be logged in to vote

You can use pywebio.session.run_js and pywebio.session.eval_js to deal with cookies or localStorage with js.

This is an example to write and read the localStorage:

key = 'name'
value = 'pywebio'

run_js("localStorage.setItem(key, value)", key=key, value=value)
val = eval_js("localStorage.getItem(key)", key=key)

Since there is no direct functions to manipulate cookies in js, you need some helper functions:

# https://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript
run_js("""
window.setCookie = function(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        exp…

Replies: 1 comment 18 replies

Comment options

You must be logged in to vote
18 replies
@mawoka-myblock
Comment options

@mawoka-myblock
Comment options

@mawoka-myblock
Comment options

@wang0618
Comment options

@mon6912640
Comment options

Answer selected by mawoka-myblock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants