chrome扩展桌面通知发送获取参数
我想将一些参数发送到我的桌面通知,但我无法检索它们。
我使用该代码打开一个通知:
var notification = webkitNotifications.createHTMLNotification(
'../html/notification.html?data='+nb
);
其中 nb 是我的变量。
notification.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>
notification.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 参数吗?
多谢。
问候
I would like to send some parameters to my desktop notification but i can't retrieve them.
I open a notification with that code :
var notification = webkitNotifications.createHTMLNotification(
'../html/notification.html?data='+nb
);
Where nb is my variable.
notification.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>
notification.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');
The result is "Vous avez" instead of "Vous avez 1 nouveau message" for example.
I checked if my notification.js is loaded.
How to check the value of "first"? How to debug that JS code?
Or have you a better method to retrieve GET parameters in URL?
Thank's a lot.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查询字符串未设置为易于使用 JS 解析。你只需要传递一个字符串吗?
如果是这样,请使用哈希而不是查询字符串:
然后在 notification.js 中:
如果您需要传递多个变量,则可以使用字符串化 JSON 对象:
然后在 notification.js 中:
如果出现解析错误,您可能会遇到使用
encodeURIComponenet()
和decodeURIComponent()
The Query String is not setup to be parsed easily with JS. Do you only need to pass one String?
If so, use the Hash instead of the Query String:
Then in notification.js:
If you need to pass more than one variable, you can use a Stringified JSON object:
Then in notification.js:
If you get parse errors you may have to use
encodeURIComponenet()
anddecodeURIComponent()