jQuery - 使用屏幕抓取推文的 GrowlUI 通知

发布于 2024-11-05 01:18:18 字数 1851 浏览 5 评论 0原文

我试图将我最新的推文拉入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-11-12 01:18:18

不确定这是否是你的问题,但看起来你的 js 字符串变量没有被引用:

echo "var test = ".$latesttweet.";\n"; 

应该是:

echo "var test = '".$latesttweet."';\n"; 

你应该转义 .$latesttweet 来处理撇号。

Not sure if this is your problem, but it looks like your js string variable is not quoted:

echo "var test = ".$latesttweet.";\n"; 

Should be:

echo "var test = '".$latesttweet."';\n"; 

You should probably escape .$latesttweet to handle apostrophes.

怪我太投入 2024-11-12 01:18:18

如果您有一个变量 test ,它是一条字符串 tweet,并且您想将该字符串传递到您的 GrowlUI 函数中...只需执行以下操作:

var test = "my string blah";
$.growlUI('Growl Notification', test);

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:

var test = "my string blah";
$.growlUI('Growl Notification', test);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文