gpt4 book ai didi

javascript - 新手调试 javascript - 提取 cookie 值并用作变量 - 当前不工作

转载 作者:行者123 更新时间:2023-11-30 18:15:34 25 4
gpt4 key购买 nike

我是新手,但我曾尝试将一些代码放在一起,但是我收到错误标记并且它似乎不起作用。我想要一些输入来帮助调试它。

我保存了一个名为“country”的 cookie,其值是“AZ”。我想使用 javascript 提取此 cookie 并将其存储为变量,然后在我的谷歌地图页面中使用它。这是我的代码:

// Setup variable with blank value
var country_code ='';
// Use read_cookie function to pull "country" value from cookie
if (country_code) var country_code = read_cookie('country');
// Make sure its value is lower case (required)
country_code = country_code.toLowerCase();

然后再往下一点:

var input = document.getElementById('loc');
var options = { types: [], ComponentRestrictions({ 'country': country_code });};
var autocomplete = new google.maps.places.Autocomplete(input, options);

第一个错误是“componentrestriction”行中的括号太多。应该怎么做?看起来我正确地关闭了它们,我用 ({}); 包裹了组件​​限制;和带有 {} 的 var 选项;

编辑:这是我在 stackoverflow 上找到的读取 cookie 函数代码:

// Reading Cookie V0.1
function read_cookie(key){
var result;
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? (result[1]) : null;
}

最佳答案

您在语句中有一个分号,并且在 ComponentRestrictions 之后有很多括号。

这样写:

var options = { 
types: [],
ComponentRestrictions: {
'country': country_code
}
};

关于javascript - 新手调试 javascript - 提取 cookie 值并用作变量 - 当前不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13359609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com