Skip to main content

Posts

Showing posts with the label Script

JavaScript - Singleton Pattern

The singleton design pattern is probably the simplest and most common pattern in JavaScript. So why should you use the singleton pattern? Encapsulation of members & functions Creates its own Namespace A singleton is a single instance object Encourages code reuse Improves readability because you can logically organise your code The point of a singleton is to only have one instance. A shopping cart is a good example of something that you may want only a single instance of at one time. The simplest form of Singleton is an object literal. This loose form of Singleton cannot be instantiated. All of the members are now accessible through the Singleton variable, accessible through dot notation. var myCart = {     self: this ,     totalCost: 0,     totalQty: 0,     cart: {},     getCart: function (){ },     updateCart: function (){ } }; alert( "Total cost: ...

HTML5 localStorage

Persistent Client-Side Key-Value Pairs The localStorage object has four primary method: localStorage. clear () localStorage. getItem ( key ) localStorage. removeItem ( key ) localStorage. setItem ( key, value ) When dealing with the localStorage object, it is important to remember that, like cookies, you are dealing with string values. Both the key and the value arguments must be strings. And, while the browser might not throw an error if you pass-in non-string values, you'll quickly find that it is simply calling the toString() method implicitly. Run the following sample code and check the console: <html> <head> <script type="text/javascript"> console.log("tim in localStorage —",("tim" in localStorage)); console.log("localStorage.getItem( tim ) —",localStorage.getItem( "tim" )); localStorage.setItem( "tim", "nice" ); localStorage[ "bad" ] = "awesome...

Google Plus +1 and share

This a html code that can be added to website to share some content on  GooglePlus You can use any image(but select a related one). By clicking on the image, content will be posted on the users G+ account. Consider the following samople code <a href="https://plusone.google.com/_/+1/confirm?hl=en&url=<LINK TO BE SHARED>" <img src="ANY IMAGE" /></a> Also the proper description will be posted, along with the link.

Facebook User Authentication: Get access to user data

This is the extension of my previous article on facebook share. But, no need of prior knowledge. Proceed from here. After registering a developer account @ https://developers.facebook.com/ , create an app. Note the App-Id and don't forget to add the redirect url under the app settings section. Also add all the access permissions you would like to get from the user under Auth Dalog section. All these permissions are to be used later to retrieve corresponding data. URL pattern to implement the authentication is as follows: http://m.facebook.com/dialog/oauth?client_id=<APP-ID> &redirect_uri=<PATH- TO-TARGETTED-LOCATION> &scope=<ALL-THE-PERMISSIONS-LIKE-email,offline_access> &response_type=token Just modify the CAPITAL lettered data, and for the redirect-url no need of using quotation marks ("")

Twitter Custom Share

We can add the tweet button directly from the twitter website. But if you want a image of you wish, then it's easy to get it. <a href="http://m.twitter.com/home?status=WHATEVER-YOU-WOULD-LIKE"> <img src="SOME IMAGE, BUT KEEP IT RELEVANT" /></a> And if you are developing any application and want to implement authentication, use OAuth provided in twitter API: https://dev.twitter.com/docs/auth/oauth Note: You have to register a developer account and create a app there. For authentication use the access token generated on the app settings page.

Facebook Share (URL based)

This is the most simplest way to add a facebook share button. Use the following code as the reference(href) attribute of your anchor tag(<a>) "http://www.facebook.com/sharer.php?u=<text or url to post on wall>" You can use any image to show the share button, but be precise in selecting it, as the user should get on it easily. Consider the following sample code: <a href="http://www.facebook.com/sharer.php?u=https://thechaithanya.wordpress.com"><img src="http://www.acrnet.org/uploadedImages/Get_Involved/facebook-logo.png" alt="share on facebook"></a> If you are developing any applicetion or website and want the post to be displayed as it was posted from your app, like " via theChaithanya ", use the following code:          "http://m.facebook.com/dialog/feed?app_id=YOUR-APP-ID&link=LINK-TO-BE-POSTED&redirect_uri=PATH-TO-LOCATION-AFTER-SHARE" On successful sharing it w...