Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Display The Image On The Page

We're making our request to Unsplash, it's returning a response that we're then converting to JSON, and now we're seeing the actual JSON data. Fantastic! All we need to do now is display the image and caption on the page.

Here's the code that I'm using:


This code will be working following way
  • get the first image that's returned from Unsplash
  • create a <figure> tag with the small image
  • creates a <figcaption> that displays the text that was searched for along with the first name of the person that took the image
  • if no images were returned, it displays an error message to the us

#HappyCoding

jQuery methods used to make asynchronous calls

jQuery has a number of other methods that can be used to make asynchronous calls. These methods are:

Each one of these functions in turn calls jQuery's main .ajax() method. These are called "convenience methods" because they provide a convenient interface and do some default configuration of the request before calling .ajax().

Let's look at the .get() and .post() methods to see how they just call .ajax() under the hood.

Running an asynchronous request in the console. The request is for a resource on SWAPI. The request is displayed in the network pane.

So we can make a request with .ajax(), but we haven't handled the response yet.

#HappyCoding!!!

Post Data From Ajax or JQuery to Servlet or Database in Java

We are going to to develop web application using jsp with java servlet using ajax and jquery, there are some times you must have to pass data from jquery or ajax to servlet and save the data in to database for future update purpose.

In JS file, you must have to add this code,


Here is the data in json data: and set the id of getting from json data when button click, its assign dynamically to button according to json data.


<button id=\"showComment\" class=\"btn btn-default\" data-postid=\""+ JSON.parse(data)[i].postid     


Here is the maincontainer is div and button postButton in inside the maincontainer. and  url:/Test/CommentServlet where Test is the application name and CommentServlet is the servlet name. and data is the post data in "postid": postid.


$("#mainContainer").on("click", "#PostButton", function() {
   var postid = $(this).data("postid");
   var my = $(this);
   $.ajax({
    "url":"/{application_name}/{Servelt_name}",
    "type" : "POST",
    "data":{"postid":postid}
   }).done(function(response) {
    if(response!=null){
     $(".alert-success").show("slow").html("Like Sucessfully");
     location.href=location.href;
    }

   })

  });


How to get data in servlet, in above we can get data which is postid in our case.



@WebServlet("/CommentServlet")
public class Like extends HttpServlet {
 private static final long serialVersionUID = 1L;
 PostService postService=new PostService();
    public Like() {
        super();
    }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  
  int userid=1;
  int postid=Integer.parseInt(request.getParameter("postid"));
//just like postid, get the other things 
//We have post id here, save here comment in to database 
                
  
   
 }

}

No need to servelt mapping in web.xml with url mapping. if you want to mapping then please don't forget to mention the corrent url in jquery file.


Happy coding !!!

Simple Mouse Mover MAZE game in JQuery

Recently, i have developed simple mouse mover MAZE game , which helpful for your career to understand how to work mouse mover in JQuery.

Here is the HTML5 Code:


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
  <title>Maze!</title>
  
  
  <link href="maze.css" type="text/css" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="maze.js" type="text/javascript"></script>
  
 </head>

 <body>
  <h1>The Amazing Mouse Maze!</h1>
  <h2 id="status">Click the "S" to begin.</h2>
  
  <!-- This part of the page represents the maze -->
  <div id="maze">
   <div id="start">S</div>
   <div class="boundary" id="boundary1"></div>
   <div class="boundary"></div>
   <div class="boundary"></div>
   <div class="boundary"></div>
   <div class="boundary"></div>
   <div id="end">E</div>
  </div>
  
  <p>
   The object of this game is to guide the mouse cursor through the 
start area and get to the end area.  Be sure to avoid the walls:
  </p>
  
  <div class="boundary example"></div>
  
  <p>
   Good luck!
  </p>
 

</body></html>



you have to write this JQuery code:

"use strict";

$(document).ready(function(){
 var start =  $("#start");
 var end = $("#end");
 var maze = $("#maze");
 var boundary = $(".boundary");
 var gameStart = false;
 maze.mouseleave(function(){ loss() });

 start.click(function(){
  $("#status").text('Move mouse to End in order to win the game');
  gameStart = true;
  if(boundary.hasClass('youlose')){
   boundary.removeClass('youlose');
  }
  boundary.mousemove(function(){ loss(); }) 
 });

 end.mousemove(function(){
  if(gameStart == true) won(); 
  else if(gameStart && boundary.hasClass('youlose'))  loss(); 
 
 });

 function won(){
  alert("Hurry!!! you won");
  gameStart = false;
  $("#status").text("Hurry!!! you won! please collect your prize money.");
 }

 function loss(){
  if(gameStart){
   gameStart = false;
   boundary.addClass('youlose');
   $("#status").text('So Sad !!!You lose the game, Try next time');
  }
 }


})


finlly, some adding of css code:

h1, h2 {
 text-align: center;
}

p {
 width: 600px;
 margin: 1em auto;
}

#maze {
 margin: auto;
 position: relative;
 height: 300px;
 width: 500px;
}

#start, #end {
 position: absolute;
 top: 205px;
 height: 30px;
 width: 30px;
 border: 1px solid black;
 padding: 5px;
 cursor: default;
 
 font-family: "Helvetica", "Arial", sans-serif;
 font-size: 25pt;
 text-align: center;
}

#maze #start {
 background-color: #88ff88;
 left: 0;
}

#maze #end {
 background-color: #8888ff;
 right: 0;
}

div.boundary {
 background-color: #eeeeee;
 border: 1px solid black;
}

div.boundary.example {
 margin: auto;
 width: 100px;
 height: 25px;
}

div.youlose {
 background-color: #ff8888;
}

/*
Hack hack hack; these are CSS "sibling selectors" for selecting
neighboring elements.  Necessary to avoid giving ids to the boundary divs
*/

#maze div.boundary {
 position: absolute;
 
 top: 0;
 left: 0;
 height: 200px;
 width: 150px;
 z-index: 1;
}

#maze div.boundary + div.boundary {
 border-left: none;
 border-right: none;
 z-index: 2;
 
 left: 151px;
 height: 50px;
 width: 198px;
}

#maze div.boundary + div.boundary + div.boundary {
 border-left: 1px black solid;
 border-right: 1px black solid;
 z-index: 1;
 
 left: 348px;
 height: 200px;
 width: 150px;
}

#maze div.boundary + div.boundary + div.boundary + div.boundary {
 border: 1px black solid;
 
 top: 250px;
 left: 0;
 height: 48px;
 width: 498px;
}

#maze div.boundary + div.boundary + div.boundary + div.boundary + div.boundary {
 border-bottom: none;
 
 top: 100px;
 left: 200px;
 height: 150px;
 width: 98px;
}


and output like this:





Happy Coding !!!


How to page redirect using jQuery?


jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.


One does not simply redirect using jQuery
jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.


For example:

// similar behavior as an HTTP redirect
window.location.replace("http://codingworkspace.com");

// similar behavior as clicking on a link
window.location.href = "http://codingworkspace.com";


Sometimes its works also,

$(window).attr("location","http://codingworkspace.com");






Open Links in New Pop off Window using Jquery


Here we want to open the links provided in the a tag in the new window.


HTML

<html>
    <head>
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
    </head>
    <body>
        <a href="http://www.google.com.com/" >click me</a>
        <a href="http://www.facebook.com/" >click me</a>
    </body>
</html>


Jquery  

$(document).ready(function(){ $("a").click(function(){ event.preventDefault(); window.open($(this).attr("href"), "My Task", width=600,height=300); }); })

Run it here Jsfiddle