gpt4 book ai didi

jquery - chrome扩展桌面通知发送获取参数

转载 作者:行者123 更新时间:2023-12-01 03:22:07 25 4
gpt4 key购买 nike

我想将一些参数发送到我的桌面通知,但我无法检索它们。

我使用该代码打开一个通知:

var notification = webkitNotifications.createHTMLNotification(
'../html/notification.html?data='+nb
);

其中 nb 是我的变量。

通知.html:

<!DOCTYPE html>
<html>
<head>
<title>Notification</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="../js/notification.js"></script>
</head>
<body>
<div id="message">Vous avez <span></span></div>
</body>
</html>

通知.js:

function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var first = getUrlVars()['data'];

$('#message span').html(first +' nouveau message');

例如,结果是“Vous avez”而不是“Vous avez 1 nouveau message”。

我检查了我的 notification.js 是否已加载。如何检查“第一”的值?如何调试该 JS 代码?或者你有更好的方法来检索 URL 中的 GET 参数吗?

非常感谢。

问候

最佳答案

查询字符串未设置为易于使用 JS 解析。只需要传递一个字符串吗?

如果是这样,请使用哈希而不是查询字符串:

var notification = webkitNotifications.createHTMLNotification('../html/notification.html#' + nb);

然后在 notification.js 中:

$('#message span').html(window.location.hash.substr(1) + ' nouveau message');

如果需要传递多个变量,可以使用字符串化 JSON 对象:

var data = {
var1: "somedata",
var2: 12345
},
notification = webkitNotifications.createHTMLNotification('../html/notification.html#' + JSON.stringify(data));

然后在 notification.js 中:

var data = JSON.parse(window.location.hash.substr(1));
$('#message span').html('var1 is ' + data.var1 + ' var2 is' + data.var2);

如果出现解析错误,您可能必须使用 encodeURIComponenet()decodeURIComponent()

关于jquery - chrome扩展桌面通知发送获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8772088/

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