Tuesday, May 28, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  95] [ 6]  / answers: 1 / hits: 27961  / 7 Years ago, wed, may 3, 2017, 12:00:00

I am not a javascript guru, but try to use js-cookie.
I included the script: https://github.com/js-cookie/js-cookie: I downloaded it (LINK), and put it in my own js file on the server.
I then include it in a test file and read some cookie, but it keeps showing me the error Cookies is not defined in the browser console. What am I doing wrong :( ?
Code:



<html><head>
<script type=javascript src=https://server/cookies.js></script>
<script>
console.log(ALL COOKIES: + Cookies.get());
</script></head>
<body></body>

More From » js-cookie

 Answers
7

I've not used the library, but a quick look at the source code shows that it exports Cookies with an uppercase C.



    if (!registeredInModuleLoader) {
var OldCookies = window.Cookies;
var api = window.Cookies = factory();
api.noConflict = function () {
window.Cookies = OldCookies;
return api;
};
}


So try using the correct case.



 console.log(ALL COOKIES:  + window.Cookies.get());


Also, everything on window is global. So you can simplify the code to this.



 console.log(ALL COOKIES:  + Cookies.get());


Next time, in the JavaScript console on the browser. Just type window and enter to see what variables are global. You can also call it directly in the console to see what happens Cookies should print out a JavaScript object with descriptions of what functions it has.



If it's undefined then it wasn't loaded or is not global.



UPDATED:



The browser is not loading the JavaScript library because the mime-type is wrong. You have to use application/javascript as here:



      <script type=application/javascript src=https://server/cookies.js></script>

[#57913] Monday, May 1, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Wed, Mar 13, 19, 00:00, 5 Years ago
;