我正在尝试将我的最新推文拉入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>
答案 0 :(得分:1)
不确定这是否是您的问题,但看起来您的js字符串变量未引用:
echo "var test = ".$latesttweet.";\n";
应该是:
echo "var test = '".$latesttweet."';\n";
你应该逃避。$ latesttweet来处理撇号。
答案 1 :(得分:0)
如果你的变量test
是一个字符串推文,并且你想将该字符串传递给你的growlUI函数......就这样做:
var test = "my string blah";
$.growlUI('Growl Notification', test);