Skip Navigation
Contrast AAA
  • Retrieving Twitter Status ("Tweets") Via Twitter API

    Posted on Saturday, September 06, 2008

    I thought it would be cool to have my Twitter status show up on the site so I looked up the Twitter API documentation and got to work. It is very simple and easy to use, especially if you are just retrieving public tweets. It allows you to retrieve the timeline(s) in different formats such as JSON, XML, RSS, etc. Since I will be making the call from the server side in ASP.NET, I chose the XML format and used LINQ to XML to get the status:

    public string GetTwitterStatus() {
    
            XDocument feed = XDocument.Load("http://twitter.com/statuses/user_timeline/r1cky.xml?count=1");
    
            return (from item in feed.Descendants("status")
                select item.Element("text").Value).SingleOrDefault();
        }
    

    As you can see, the code is very similar to the code for retrieving my delicious RSS feed. Be sure to cache the result because Twitter throttles the use of the API.

    If you are going to do more complex actions, like updating your status or looking up private/protected timelines, then you need to authenticate. At this point, the authentication is just Basic HTTP Auth so its not very secure but not hard to implement either ;).

    2 comments... Read Comments | Leave Your Comment
  • Firefox is dead... VIVA LA FIREFOX!

    Posted on Wednesday, September 03, 2008

    Like every other web developer/professional and their grandma, I downloaded and installed the shiny new Google Chrome browser as soon as I found out it was available. The install was very fast and painless, I even missed the option where you can tell it not to import settings from your default browser (argh)...

    Surfing the web with Chrome is a treat. The internet(s) feel so much faster and the browser itself, being so simple and minimal, stays out of the way. My feeling is that this browser is game changing. If there was any remaining doubt about JavaScript, this squashes it and takes the language to the next level for developing full blown enterpise applications.

    My first test was to try my favorite IM application - meebo, which I know is a resource hog. FF3 is consuming 100K+ of RAM to run meebo while Chrome is consuming 28K for meebo, 24K for the Browser and 6.5K for Flash (I guess meebo is using Flash), for a total of less than 60K. That is a very significant improvement.

    Not only is this browser nicer to my RAM, it is very speedy (orders of magnitude better than the other browsers out there).

    Will this new browser be able to gain a significant market share of the browser market? If anything, this browser will push the other browsers vendors to improve their products. But I believe Chrome will be here to stay. In less than a day, it has almost 3% market share according to GetClicky.

    Everybody's guess is that Chrome will eat into Firefox's market share. This might be true at first, since the technical community will be the early adopters. But if there is anybody that can get IE6 users to try out a new browser, it is Google. And I hope they succeed at doing so!

    Will I be using Chrome? No doubt I will. At a minimum to run meebo so that I can save some resources on my laptop. Will it completely replace my Firefox usage? No way! At least not right now. It is still missing the Addon architecture. I can't live without Firebug, the Delicious toolbar, the web dev toolbar, http live headers, add n edit cookies, etc. I am sure it will get there, but for now I will split time between the browsers.

    Update (9/3/08): John Resig got busy and did some performance testing.

    Be the first to respond... Leave Your Comment
  • Upgraded to ASP.NET MVC Preview 5

    Posted on Friday, August 29, 2008

    I just upgraded the site to the latest (pre)release of the ASP.NET MVC Framework. It was quick and painless and seems to be working great. All I had to do was swap out the assemblies and change some of the assembly versions in the Web.config from 0.0.0.0 to 3.5.0.0.

    I love when upgrades are this painless. Now I'll go check out what they added. I will start with Haack's post: How a Method Becomes An Action. I am also expecting the mandatory ScottGu 12 page post on the release :).

    Be the first to respond... Leave Your Comment
  • The Opposite of the NOSCRIPT Element (YesScript? / ScriptOnly?)

    Posted on Thursday, August 28, 2008

    The HTML NOSCRIPT element is very simple and straightforward:

    18.3.1 The NOSCRIPT element

    The NOSCRIPT element allows authors to provide alternate content when a script is not executed. The content of a NOSCRIPT element should only be rendered by a script-aware user agent in the following cases:

    • The user agent is configured not to evaluate scripts.
    • The user agent doesn't support a scripting language invoked by a SCRIPT element earlier in the document.

    User agents that do not support client-side scripts must render this element's contents.

    There is no HTML element that will do the opposite of this. The SCRIPT element will execute its contents, not render it. Of course, we could use document.write() to insert content in the page only when JavaScript is enabled:

    <script type="text/javascript">
    //<![CDATA[
        document.write("<p>Hello World! JavaScript is enabled.</p>");
    //]]>
    </script>
    <noscript>
        <p>Hello World! JavaScript is disabled.</p>
    </noscript>
    
    

    That will work just fine in some cases, but it isn't valid XHTML and it breaks when serving the page with the XHTML/XML MIME type. Plus, it is just nasty and ugly looking... :)

    Does document.write work in XHTML?

    No. Because of the way XML is defined, it is not possible to do tricks like this, where markup is generated by scripting while the parser is still parsing the markup.

    You can still achieve the same effects, but you have to do it by using the DOM to add and delete elements.

    As stated above, there are DOM method alternatives that can be used to implement this functionality. But again, this creates a mix of HTML and JavaScript that is unmaintainable (especially if the content is data driven and/or being managed by business users).

    A cleaner way would be to include the content in the page as you normally would, have it hidden by CSS and then show it via JavaScript. If there is no JavaScript, then it will not be rendered.

    HTML:

    <p class="YesScript">
        Hello World! JavaScript is enabled.
    </p>
    

    CSS:

    .YesScript { display:none; }
    

    JavaScript (jQuery syntax):

    $(document).ready(function(){
        $('.YesScript').show();
    });
    

    This does the trick, but in some cases (especially IE) the page will render before the JavaScript, causing the page to "flicker" when the content all of a sudden appears. This is very undesireable, as it can cause epileptic seizures... Uhm, on a more serious note, it is just very annoying to the eye. So, what if we could apply CSS rules based on the availability of JavaScript? Here is how to do it with one line of JavaScript in the HEAD of the HTML document:

    HTML:

    <html>
        <head>
            <title>...</title>
            <script type="text/javascript">
            //<![CDATA[
                document.getElementsByTagName('html')[0].className='jsOn';
            //]]>
            </script>
        </head>
        <body>
            <p class="YesScript">
                Hello World! JavaScript is enabled.
            </p>
        </body>
    </html>
    

    CSS:

    .YesScript { display:none; }
    .jsOn .YesScript { display:block; }
    

    This is the best solution I have found. You add a class to the HTML element via JavaScript in the HEAD of the document and then use that to target your CSS rules and styling. This can also be used to hide elements that you need to have in the DOM but don't want them to initially render (elements declared in the NOSCRIPT tag will not be available in the DOM if JavaScript is enabled). A use case for this would be for Tab content (you want all the panels hidden except the active one).

    Cheers!

    Be the first to respond... Leave Your Comment
  • JavaScript Cookie Utility

    Posted on Wednesday, August 27, 2008

    Today once again, I had to write some JavaScript to fix some backend bugs... I love how some backend devs feel everything can and should be fixed in JavaScript :-/

    Basically, I had to create, read and update cookies based on the products the user is browsing. So I created this little utility to do the job:

    var CookieUtil = {
      createCookie:function(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
      },
      readCookie:function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
      },
      eraseCookie:function(name) {
        createCookie(name,"",-1);
      }
    };
    
    Be the first to respond... Leave Your Comment

More Posts:

  1. 1
  2. 2
  3. 3
unable to retrieve twitter status :( --- more tweets

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

374 ms | login | html5 | top