Thursday, December 10, 2009

JSONP

HI

Here i will show you how jsonp works.
Let's first dive into details and then later we gonna see an example on jsonp.

DETAILS:


"JSONP" stands for JavaScirpt Object Notations with Padding.
we already know JSON is one of the data formats used on web.
and we also know that AJAX doesn't allow us to fetch data from cross-Domain sites.
(here Ajax is Browser's Native Object and not RIA's Object).
ofcourse it's possible with RIA controls on Browser like Silverlight, Flash using policy files.


For work-around solution to this restriction, JSONP comes into the scene.
JSONP is not a new technology, it's a simple idea, (just like AJAX,) it uses existing technologies.
JSONP is used for data fetching (in JSON Format) from cross-Domain and same-Domain sites using
"javascript" tag's "src" attribute.


After data has been received, it calls a callback function, which is padded to the URL value of "src" attribute.
Confused? see the example below.


VULNERABILITIES:

JSONP vulnerable to cross site scripting attacks (XSS).
so don't use it for sensitive data fetching.
and use it with trusted sites only.




EXAMPLE:


In this example, i will show you how to fetch data from same-Domain site using asp.net.
>create a new asp.net project and name it 'TestSite" and open it.
>create a new folder called "jsonp".
>create a new html file under jsonp folder, and name it "jsonp.html"
>and write this code in it.


<htmL>
<head>
<script type="text/javascript">
function callme(obj)
{
txt.value="value one:"+obj.two[0];
txt.value+=" value two:"+obj.two[1];
txt.value+=" ";
txt.value+="Obj one val:"+obj.one;
}
</script>
</head>
<body>
testing jsonp! <i>callback function should be defined first, then get jsonp using javascript src.</i><br/>
<textarea id="txt" cols="20" rows="4">
</textarea>
<script type="text/javascript" src="http://localhost:1049/TestSite/JsonP/Handler.ashx?jsonp=callme">
</script>
</body>
</html>

Now create an asp.net handler file under jsonp folder, and name it, "jsonp.ashx"


using System;
using System.Web;

public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/javascript";
string callbackFuncName=context.Request.QueryString["jsonp"].ToString();
string json = "{'one':'rozyrojz','two':[1,2]}";
context.Response.Write(callbackFuncName+"("+json+");");
}
public bool IsReusable {
get { return false; }
}
}

Now run the jsonp.html. That's it!

REFERENCES:
***> http://neil.fraser.name/news/2009/07/27/
**> http://www.west-wind.com/Weblog/posts/107136.aspx
*> http://niryariv.wordpress.com/2009/05/05/jsonp-quickly/


Thanks for reading!

Tuesday, October 13, 2009

Toggle DIV Buttons / Tabs in HTML

Hi,

     Here i am going to show you how to implement Tabs functionality in a webpage.
From below screenshots, we can see that the webpage contains 4 DIV tags:

  1. Heading

  2. Left Tab

  3. Right Tab

  4. Content Container
The Main container's content will be changed, whenever Tab Buttons clicked.
Below is the screenshot from IE:


Below is the screenshot from Firefox:

Explanation:
Some style properties work good in IE, But not get rendered as expected in other browsers. Here i will explain, how to solve 'float' style property for DIV boxes in firefox (and chrome). when we modify the style properties for these 3 DIV's, [ 2 buttons, 1 container], one among these 2 browsers seems to render incorrectly when other does render correctly.

To make them show correctly in both browsers and
For solving these Bugs, iam going to use, 'Conditional comments' which are IE specific. For details check here.

Below is the CSS and JavaScript for the layout:

<style type="text/css">
body{ padding:0px; margin:0px; }
</style>
<script type="text/javascript">
var stmt1="line one, line two, line three<br />line four<br />line five<br />line six";
var stmt2="<br/> TEXT CHANGED !<br/>";
var tgl=false;
function toggle(me)
{
var al=document.getElementsByTagName("BUTTON");
for(var i=0;i<al.length;i++)
al[i].style.backgroundColor="Red";

me.style.backgroundColor="Pink";
document.getElementById("divMain").innerHTML=(tgl)? stmt1 : stmt2;
tgl=!tgl;
}
</script>

Below is the markup for the layout:


<div style="background-color:blue;text-align:center;margin-bottom:5px;font-weight:bold;color:white;">heading</div>

<div style="background-color:pink;width:70px;height:30px;float:left;">
<button style="background-color:pink;width:70px;height:30px;text-align:center;border-width:0px;font-weight:bold;color:white;" onclick="toggle(this);">clickMe</button>
</div>
<div style="width:70px;height:30px;float:left;margin-left:10px;">
<button style="background-color:red;width:70px;height:30px;text-align:center;border-width:0px;font-weight:bold;color:white;" onclick="toggle(this);">clickMe</button>
</div>
<!-- tweak for IE, otherwise gap will appear.-->
<!--[if IE]>
<div style="clear:right;"> </div>
<![endif] -->
<div id="divMain" style="background-color:pink;clear:both;"> <!-- div clear style-property tweak for firefox,chrome-->
line one, line two, line three
<br />line four<br />line five<br />line six
</div>


If you know any other solution, fixes, share with me.

Check the Demo here!

Thursday, October 8, 2009

JavaScript Prototype Object

Before proceeding any further, you should know how to create custom Objects in javascript. If u don't know, read this post.
Prototype Object allows us to add new member to already created/existing Object.
prototype object can be used on any object type:
  • js built-in objects - String, Image etc.
  • custom Objects ( template type).

Now here we will discuss applying prototypes on custom objects:

prototype can be used only on template type objects. which means, objects which allows instance creation by 'new' keyword.
ex:1:

function person(firstname,lastname,age){this.firstname=firstname;this.lastname=lastname;this.age=age; }
var obj=new person("rozy","rojz",24);
person.prototype.pi="3.14";
alert(obj.pi);


ex:2:

function Obj(){};
Obj.prototype = new Array();
Obj.prototype.fn = function(){alert('new method fn of Obj');};
var o = new Obj(); // o is a instance of Obj
o.push(one); alert(o.length);
o.fn();

Prototypes can't be appled on singleton types, since, any new member can be added to it any time directly.
ex: var personObj=new Object();
personObj.firstname="xxx";
personObj.lastname="yyy";


for further reading/reference: click here.

Tuesday, October 6, 2009

JavaScript Object Creation

What i observed is, we can create custom Objects in JavaScript in 3 ways:
  1. singleton
  2. singleton compact (used in json)
  3. template (class).

Now i will show examples for each:

1. Singleton - once created, no further independent instances are possible from it.

var personObj=new Object();

personObj.firstname="rozy";personObj.lastname="rojz";

personObj.age=50;personObj.eyecolor="blue";

personObj.eat= function(val){ txt.value+=val; };

2. Singleton Compact - similar to above, but useful in JSON format.

var newObj={name:'rozy',age:5,eat:function(){txt.value+='eat';}};

3. Template / Class - It enables multiple instances can be created.

function person(firstname,lastname,age,eyecolor){

this.firstname=firstname;this.lastname=lastname;

this.age=age;this.eyecolor=eyecolor;this.newlastname=newlastname; }

function newlastname(new_lastname){ this.lastname=new_lastname; }

Rounded Corner Box Model and +

This is my first blog post. :)
Here i will discuss about the corners styling for the box models or div tags used in HTML.
I have created a javascript template class (js library called CBox.js), which easily creates the rounded-cornered colored Box. Using it we can create 3 types of corners: convex, concave and strike.
This library creates the Table layout and uses graphics code take from here .
It works only on colored Boxes and allows greater extending of corners.
It has pros and cons, check below and it works on all major browsers.



Please Note: that it creates style-corner colored Box model, not simple corner Border.

Usage Example:
<html>
<head>
<script type="text/javascript" src="Demo\CBox.js"></script>
<script>
window.onload=function(){
var content="Few Lines: Line one, line two, line three etc.";
var obj=new CBox("div1",content,100,150,null,"#ffccdd",1,20);
obj.Draw();  }
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>

API documentation of library:
CBox(Container,Content,Box_height,Box_width,Page_color,Box_color,Corner_Type,Corner_Extent)
Container ->Box holder
Content ->content to be displayed in Box (usually text).
Box_height ->height of the Box
Box_width ->width of the Box
Page_color ->background color used for WebPage or Container, if any, otherwise set to null.
Box_color -> color compulsory. should not be null.
Corner_Type -> corner style, should be one of these: 1 (convex) or 2 (concave) or 3 (strike).
Corner_Extent ->area of corner-style, should be greater than or equal to 20.
- colors can be Hexa or NamedColors.
- Box should be colored.

Pros and Cons:
-Javascript browser detection and huge code for graphics.
- Box must be Colored.
- for Opera browser Corner_Extent will always be 25. may not suit for all webpages. (If (div) Container is floating, then reinvoke CBox() under window.onresize event)
- 3 Corner styles to choose from.
- Corners area can be extended.
- works on all major (known) browsers. (as of this date).

Download and Demo:
Check the demo here!
Download the Zip file here!

Corner/Curve bordered Box:
We can easily create rounded-corner bordered box model for firefox, chrome etc. browsers using this simple CSS:

.curved {-moz-border-radius:10px;

-webkit-border-radius:10px;}

But it will not work in IE, and neither IE has any simple CSS option.

for work around solution, try below simple HTML:
Store 1 <fieldset>
<legend>Store 1</legend>
<label for="data">Data:</label>
<textarea id="data"> some data here </textarea>
</fieldset>

ofcourse we don't get greater extent of rounded corner with it.
Another solution for simple Round-corner Border, which works in IE, check this.