Search code examples
phpjavascriptjquerygrowl

jQuery - growlUI Notification using screen scraped tweet


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>

Solution

  • 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.