gpt4 book ai didi

javascript - session.js如何访问访问者数据暴露为Json?

转载 作者:行者123 更新时间:2023-11-29 10:51:50 27 4
gpt4 key购买 nike

我已将此 js 文件包含在我的页面标题中以存储访问者信息:

https://github.com/codejoust/session.js/

我想做的是用它来获取访问者信息,然后在隐藏的表单字段中设置该信息并将它们传递给服务器:

例如,我要提取的 JSON 部分之一如下所示:

"browser": {
"browser": "Chrome",
"version": 16,
"OS": "Mac"
},

我很困惑的是如何访问这些数据。

最初我只是将 js 文件包含在页面的 head 部分并访问 window.session.browser.browserwindow.session 等元素.browser.OS 这工作正常。

但是更新原js文件后,这个方法就失效了,在Firebug中报错:

window.session is undefined

最佳答案

数据存储在window.session.browser中:

var oBrowserData = window.session.browser;
document.getElementById('os').value = oBrowserData.OS;
document.getElementById('browser').value = oBrowserData.browser;
document.getElementById('version').value = oBrowserData.version;

另见 example .

要访问数据,您可以这样做:

<body>
<form>
<input type="hidden" id="os" name="os" value="" />
<input type="hidden" id="browser" name="browser" value="" />
<input type="hidden" id="version" name="version" value="" />
</form>
<script type="text/javascript">
window.session = {
start: function(session) {
document.getElementById('os').value = session.browser.os;
document.getElementById('browser').value = session.browser.browser;
document.getElementById('version').value = session.browser.version;
},
config: { gapi_location: false }
};
</script>
<script type="text/javascript" src="http://codejoust.github.com/session.js/session-0.4.js"></script>
</body>

另见我的新 example .

CodeJoust 说:

If you include the session-0.4.js, not session.js, the API will remain stable.

Also, both using the callback and the script tag in the body is optional, as you're not waiting for a location callback. However, adding it to the end of the body works well, as the form tag will be avaliable to modify after the session.js script is loaded.

关于javascript - session.js如何访问访问者数据暴露为Json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8772934/

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