Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript cookies all together


window.onload = () => {
    main()
}

let main = () => {
    let setName = document.querySelector('#setName')
    let setusername = document.querySelector('#setUsername')
    let getname = document.querySelector('#getname')
    let get_email = document.querySelector('#getUsername')

    //set name 
    setName.addEventListener('click', setnameFn('name', 'mohammed nazim', 2))

    //set userName 
    setusername.addEventListener('click', setnameFn('email', 'mohammednazim3629@gmail.com', 3))

    //get name
    getname.addEventListener('click', getnameFn('name'))

    //get email
    get_email.addEventListener('click', getnameFn('email'))
}


/**
 * set name function defined here
 */
let setnameFn = (cname, cValue, exdays) => {
    let expaired
    return () => {
        let date = new Date()
        date.setTime(date.getTime() + (exdays * 24 * 60 * 60 * 1000));
        expaired = expaired + date.toUTCString()

        document.cookie = cname + '=' + cValue + ';' + expaired + ";path=/"
    }
}

//['email=mohammednazim3629@gmail.com', ' name=mohammed nazim']
let getnameFn = (cname) => {
    return () => {
        let getData = document.cookie.split(';')
        let nameLength = cname + '='

        for (let i = 0; i < getData.length; i++) {
            let usernames = getData[i]

            while (usernames.charAt(0) === ' ') {
                usernames = usernames.substring(1)
            }
            if (usernames.indexOf(nameLength) === 0) {
                alert(usernames.substring(nameLength.length))
            }

        }

        // the (return) will be execute if the (for loop) dosen't execute
        return ''
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js redirection captive portal 
Javascript :: This Refers To The Window Object Here 
Javascript :: add defer in tag manager 
Javascript :: Javascript Encapsulation Inheritance Polymorphism Composition 
Javascript :: A Node Module For ReactJS 
Javascript :: javascript quotes 
Javascript :: maptable elo 
Javascript :: electron write to csv 
Javascript :: Create Built-in AbortController Object 
Javascript :: Object.entries() To Use For Of On JSON 
Javascript :: success res node.js 
Javascript :: Javascript twoSum algorithm: Given an array of integers, return indices of the two numbers such that they add up to a specific target 
Javascript :: general hardhat config js file code 
Javascript :: path.join javascript one folder above 
Javascript :: errors thrown inside asynchronous functions will act like uncaught errors 
Javascript :: PostManDocs 
Javascript :: Backbone Render 
Javascript :: &quot in json 
Javascript :: Backbone Add To Collection 
Javascript :: Backbone View Notes 
Javascript :: java script return array 
Javascript :: javascript asynchronous function list 
Javascript :: fs 
Javascript :: how to scroll automatically to the bottom of the page using javascript 
Javascript :: jsx attributes 
Javascript :: moment js remove seconds 
Javascript :: Manage selection fabric js 
Javascript :: how to assert element attributes in testing library 
Javascript :: JavaScript for loop Display Numbers from 1 to 5 
Javascript :: reading an array from python to js 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =