jQuery - 使用屏幕抓取推文的 GrowlUI 通知
我试图将我最新的推文拉入 JavaScript 中的变量中,然后使用 GrowlUI() 在 jQuery 的通知中显示它。
目前,我已使用 PHP 将我的推文拉入 JavaScript 中名为 test 的变量中。我需要调整以下 jQuery 代码以使用“test”而不是“Hello”。
当前代码有效:
$.growlUI('Growl Notification','Hello');
尝试的代码无效:
$.growlUI('Growl Notification', $(test));
我的问题
如何使用变量 test(一个 javascript 变量)作为 jQuery 属性?
非常感谢!
我的源代码:
<html>
<head>
<!-- Styling for growlUI -->
<style type="text/css">
div.growlUI { background: url(check48.png) no-repeat 10px 10px }
div.growlUI h1, div.growlUI h2 {
color: white; padding: 5px 5px 5px 75px; text-align: left
}
</style>
<!-- Import jquery from online -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<!-- Import blockUI -->
<script type="text/javascript" src="js/jquery.blockUI.js"></script>
<script type = "text/javascript">
<!-- Screenscrape using PHP, put into javascript variable 'test' -->
<?php
$url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=XXXXXXXXXX";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
$start = strpos($content,'<text>');
$end = strpos($content,'</text>',$start);
$latesttweet = substr($content,$start,$end-$start);
echo "var test = ".$latesttweet.";\n";
?>
</script>
</head>
<!-- Here I'm attempting to use test variable in second half of growlUI parameters -->
<body onLoad="$.growlUI('Latest Update',test); ">
</body>
</html>
I am trying to pull my latest tweet into a variable in javascript, then use growlUI() to display this in a notification in jQuery.
Currently, I have pulled my tweet using PHP into a variable in javascript called test. I need to adjust the following jQuery code to use "test" instead of "Hello".
Current code works:
$.growlUI('Growl Notification','Hello');
Attempted code does not work:
$.growlUI('Growl Notification', $(test));
My Question
How do I use the variable test, which is a javascript variable, as a jQuery attribute?
Many thanks!
My source code:
<html>
<head>
<!-- Styling for growlUI -->
<style type="text/css">
div.growlUI { background: url(check48.png) no-repeat 10px 10px }
div.growlUI h1, div.growlUI h2 {
color: white; padding: 5px 5px 5px 75px; text-align: left
}
</style>
<!-- Import jquery from online -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<!-- Import blockUI -->
<script type="text/javascript" src="js/jquery.blockUI.js"></script>
<script type = "text/javascript">
<!-- Screenscrape using PHP, put into javascript variable 'test' -->
<?php
$url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=XXXXXXXXXX";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
$start = strpos($content,'<text>');
$end = strpos($content,'</text>',$start);
$latesttweet = substr($content,$start,$end-$start);
echo "var test = ".$latesttweet.";\n";
?>
</script>
</head>
<!-- Here I'm attempting to use test variable in second half of growlUI parameters -->
<body onLoad="$.growlUI('Latest Update',test); ">
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否是你的问题,但看起来你的 js 字符串变量没有被引用:
应该是:
你应该转义 .$latesttweet 来处理撇号。
Not sure if this is your problem, but it looks like your js string variable is not quoted:
Should be:
You should probably escape .$latesttweet to handle apostrophes.
如果您有一个变量
test
,它是一条字符串 tweet,并且您想将该字符串传递到您的 GrowlUI 函数中...只需执行以下操作:If you have a variable
test
that is a string tweet, and you want to pass that string into your growlUI function...just do it: