函数不返回变量

发布于 2024-11-08 07:36:45 字数 916 浏览 0 评论 0原文

我有一个函数,它接受一个参数(键)来从 cookie 中检索它的值。我在需要特定信息的任何地方调用该函数。除非一切都返回为未定义。

//before $(document).ready();
var keyval = ""; //VARIABLE FOR PASSING COOKIE VALUE
var getCookieVal =function(c_name){
var cleanCookie = document.cookie.substr(0, document.cookie.indexOf("; __utma="));//REMOVES EXTRA INFORMATION
var cookieArr = cleanCookie.split(";");//MAKES AN ARRAY OF EACH PAIR
    $.each(cookieArr, function(index, val){
        var valArr = val.split("=");//SPLITS THE KEY VALUE PAIR INTO AN ARRAY
        var key = valArr[0];
        keyval = valArr[1];
        if (key == c_name){
            alert(keyval);//ALERTS CORRECT ANSWER
            return keyval;
        }
    });
    console.log(keyval);//RETURNS UNDEFINED
}

//IN ANOTHER FILE I CALL THE FUNCTION:
$(document).ready(function(){
    getCookieVal("username");
    alert(keyval);//RETURNS UNDEFINED
});

有人知道我做错了什么或者我如何获得这个值?

I have a function that takes a parameter(key) to retrieve it's value from a cookie. I call that function anywhere that I need a specific bit of information. except everything comes back as Undefined.

//before $(document).ready();
var keyval = ""; //VARIABLE FOR PASSING COOKIE VALUE
var getCookieVal =function(c_name){
var cleanCookie = document.cookie.substr(0, document.cookie.indexOf("; __utma="));//REMOVES EXTRA INFORMATION
var cookieArr = cleanCookie.split(";");//MAKES AN ARRAY OF EACH PAIR
    $.each(cookieArr, function(index, val){
        var valArr = val.split("=");//SPLITS THE KEY VALUE PAIR INTO AN ARRAY
        var key = valArr[0];
        keyval = valArr[1];
        if (key == c_name){
            alert(keyval);//ALERTS CORRECT ANSWER
            return keyval;
        }
    });
    console.log(keyval);//RETURNS UNDEFINED
}

//IN ANOTHER FILE I CALL THE FUNCTION:
$(document).ready(function(){
    getCookieVal("username");
    alert(keyval);//RETURNS UNDEFINED
});

Anyone know what I did wrong or how I can get that value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁我热情 2024-11-15 07:36:45

开始吧:

var getCookieVal = function(key) {
    var coo = document.cookie;
        arr = coo.substr(0, coo.indexOf('; __utma=')).split(';'),
        value;
    $.each(arr, function(i, v) {
        var valArr = v.split('=');
        if ( valArr[0] === key ) {
            value = vallArr[1];
            return false;
        }            
    });
    return value;
};

$(document).ready(function() {
    var keyval = getCookieVal('username');
    alert(keyval);
});

因此,您在 if 分支内返回 false(一旦找到所需的值)即可跳出 $.each 循环。然后,您只需从 getCookieVal 函数返回值即可。

请注意,这里不需要定义全局keyval变量。

Here you go:

var getCookieVal = function(key) {
    var coo = document.cookie;
        arr = coo.substr(0, coo.indexOf('; __utma=')).split(';'),
        value;
    $.each(arr, function(i, v) {
        var valArr = v.split('=');
        if ( valArr[0] === key ) {
            value = vallArr[1];
            return false;
        }            
    });
    return value;
};

$(document).ready(function() {
    var keyval = getCookieVal('username');
    alert(keyval);
});

So you return false inside the if-branch (once you find the desired value) to break out of the $.each loop. Then, you just return value from the getCookieVal function.

Note that there is no need to define a global keyval variable here.

你的他你的她 2024-11-15 07:36:45

此行:

return keyval;

...从您传递给 $.each 的匿名函数返回,而不是从您的 getCookieVal 函数返回。

This line:

return keyval;

...returns from the anonymous function you passed to $.each, not from your getCookieVal function.

随梦而飞# 2024-11-15 07:36:45

您需要返回 false 才能跳出循环。否则,循环将继续,keyval 被覆盖。您正在返回keyval,这不会破坏循环。将其更改为 false 就可以了。

You need to return false to break out of your loop. Otherwise, the loop continues and keyval is overwritten. You are returning keyval, which will not break the loop. Change that to false and you should be good.

紧拥背影 2024-11-15 07:36:45

您的代码可以简化为这样(假设 getCookieVal 的调用者是可信的)

function getCookieVal(name)
{
    var pattern = new RegExp('(?:^|;)' + name + '=([^;]*)', 'i'),
         matches = pattern.exec(document.cookie);
    return matches ? matches[1] : null;
}

$(document).ready(function() {
    var keyval = getCookieVal('username');
    alert(keyval);
});

Your code could be reduced to this (assuming that caller of getCookieVal is trusted)

function getCookieVal(name)
{
    var pattern = new RegExp('(?:^|;)' + name + '=([^;]*)', 'i'),
         matches = pattern.exec(document.cookie);
    return matches ? matches[1] : null;
}

$(document).ready(function() {
    var keyval = getCookieVal('username');
    alert(keyval);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文