最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

"Cookies not defined" in standalone js-cookie javascript lib? - Stack Overflow

programmeradmin3浏览0评论

I am not a javascript guru, but try to use js-cookie. I included the script: : 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>

I am not a javascript guru, but try to use js-cookie. I included the script: https://github./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>
Share Improve this question edited May 3, 2017 at 13:42 edbras asked May 3, 2017 at 13:33 edbrasedbras 4,40411 gold badges54 silver badges80 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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>

You are using wrong versions of cookie.js on different routes/pages

Use the Latest

<script src="https://cdn.jsdelivr/npm/js-cookie@2/src/js.cookie.min.js"></script>

If it is not working, then try

<script src="https://cdn.jsdelivr/npm/js-cookie@rc/dist/js.cookie.min.js"></script>

Quick fix : Just use window. before calling it

window.Cookies.get()

Inspired by @ucMedia's answer, you can add the following line at the beginning of a script to fix any issues.

var Cookies = window.Cookies;
发布评论

评论列表(0)

  1. 暂无评论