简体   繁体   English

将JavaScript函数存储在cookie中?

[英]Store JavaScript function in cookie?

Is it possible to save a JavaScript function in a cookie? 是否可以将JavaScript函数保存在cookie中? If so, how? 如果是这样,怎么办?

For example, I have user settings for keyboard shortcuts, but the behavior associated with each keystroke could be custom. 例如,我具有键盘快捷键的用户设置,但是与每个击键相关的行为都可以自定义。 I was hoping to save these potentially unique and user-specific mappings in a cookie. 我希望将这些潜在的唯一和用户特定的映射保存在Cookie中。

I'm currently using Angular 1.4.7 to put the cookie. 我目前正在使用Angular 1.4.7放置cookie。 The data it returns strips out the function. 它返回的数据去除了该功能。

You shouldn't save the function in a cookie. 不应将函数保存在Cookie中。 Instead, you should create an object with all the possible methods the user can execute, and instead store the method name, ie: 相反,您应该使用用户可以执行的所有可能方法创建一个对象,并存储方法名称,即:

var customUserFunctions = {
    someMethod: function() { ... },
    ...
};

And then store "someMethod" in the cookie, and call it with customUserFunctions[variableHoldingSomeMethodString]() . 然后将"someMethod"存储在cookie中,并使用customUserFunctions[variableHoldingSomeMethodString]()调用。

That being said , this is how you would store the function in a cookie: 话虽如此 ,这就是将函数存储在cookie中的方式:

// Define your function
var myFunc = function() { console.log("I'm a stored function!"); };
// Turn it into a string
var myFuncString = myFunc.toString();
// Store it in your cookie
document.cookie = myFuncString;
// get your function back
var myCookieFunc = eval(document.cookie);
// call it
myCookieFunc();

Note that this will create a security vulnerability in which an attacker can modify a cookie and your code will execute whatever function(s) they put in there. 请注意,这将创建一个安全漏洞,攻击者可以在其中修改Cookie,并且您的代码将执行他们放置在其中的任何功能。

No, you can't store a function on a cookie. 不,您不能在Cookie上存储函数。 You could, though, store a reference to the function in an associative array, and store the key in a cookie instead. 但是,您可以将对函数的引用存储在关联数组中,然后将键存储在cookie中。 That way, when the cookie is loaded, use the key to 're-load' the function. 这样,当加载cookie时,使用键“重新加载”功能。

EDIT: 编辑:

Building on my first answer, the ideal (IMO) solution would be to define a factory which provides the same kind of functionality. 基于我的第一个答案,理想的(IMO)解决方案将是定义一个提供相同功能的工厂。 That way it can be injected into your modules as required. 这样就可以根据需要将其注入到您的模块中。 For example, you could do (rough demo): 例如,您可以做(粗略的演示):

app.factory('getUserFunctions', function(){
    return function(keyFromCookie){
      switch (keyFromCookie) {
        case "hello": return function(){
            return "Hello, world!";
        }
        case "goodbye": return function() {
            return "Goodbye, crule world";
        }
        default: return undefined;
      }
    };
});

and call the factory with a token to get a reference to one of the functions you defined: 并使用令牌调用工厂以获取对您定义的功能之一的引用:

$scope.fromFactory = getUserFunctions("goodbye");

You can try to use the Function object. 您可以尝试使用Function对象。 Save the content of the function in the cookie than retrieve it, than pass it to the constructor. 将函数的内容保存在cookie中,而不是检索它,然后将其传递给构造函数。

var str = "{alert(1)}";
var f = new Function(str);
f();

Fiddle: https://jsfiddle.net/d37qzet6/ 小提琴: https : //jsfiddle.net/d37qzet6/

But I don't think this is the best approach in this scenario. 但是我认为这不是这种情况下的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM