Friday, April 15, 2011

Stop Parent event from firing

One funny problem you might face in this web world, you might add click event to parent and also add click event to child, but when you click on child element it also fires parent event :(

don't worry :) with just single line of Javascript code you can stop parent from being access to the child.

Here we go...

<div id="parent" onClick="imYourBoss()">
     <div id="child" onClick='imEmployee()'>

in above example you can stop your parent by using following code :
     alert("Im your BOSS");

     alert("Yeah I stop my boss from accessing me :)");

using stopPropagation technique we can prevent any parent handlers from being notified of the event.

stay tune for latest stuff :)

Wednesday, February 9, 2011

Solved Cross Domain Issue

Now its very easy to fix cross-domain issue using JSONP
JSONP allows you to make an HTTP request outside your own domain
Following example will help you :
Suppose your url returns JSON data as :

            { "gender":"Male","website":""}

 then just wrapped this JSON inside the callback function on server side
 here we use callback function name as "functionName"

            functionName({ "Gender":"Male","website":""})

  now just create function on client side with this callback name

     function functionName(result) {
            alert(" Gender : "+result.gender);

  Now its time to use JQuery, call your url using $.getJSON function,here is the  example 
<script src=""></script>
                  function(data){ }

         function functionName(data) {
               alert("Symbol: " + data.token + ", Price: " + data.price);

 Server side changes :
 You also need to change your server side code
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
  throws ServletException, IOException {
 String jsonData = getDataAsJson(req.getParameter("symbol"));
 String output = "functionName(" + jsonData + ");";
 PrintWriter out = resp.getWriter();
 // prints: jsonp1232617941775({"Gender":"Male","website":""});