Showing posts with label jQuery. Show all posts
Showing posts with label jQuery. Show all posts

Monday, 28 March 2016

Why jQuery is not working properly with Struts2 application ?


Problem: jQuery is not working properly with Struts2 application.

Solution: Remember following 2 points :
1. Use s:head with theme="ajax" tag inside HTML head tag.
<head>
  <s:head theme="ajax" debug="true" />
</head>

2. Use scripts for jQuery inside body
<body>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
  </script>
  <script>
  $(document).ready(function(){
    $("#flip").click(function(){
      $("#panel").slideToggle("slow");
    });
  });
  </script>
  ....
  ....
</body>

How to use other JavaScript frameworks togather with jQuery ?


jQuery and other JavaScript frameworks

jQuery uses the $ sign as a shortcut for jQuery, there could be conflict if other JavaScript frameworks also use the $ sign as a shortcut.

Other popular JavaScript frameworks are :
  • MooTools
  • Backbone
  • Sammy
  • Cappuccino
  • Knockout
  • JavaScript MVC
  • GWT (Google Web Toolkit)
  • Google Closure
  • Ember
  • Batman
  • Ext JS
Some of the frameworks also use the $ character as a shortcut (just like jQuery), using them togather might result in that your scripts stop working.


noConflict()

noConflict() method in jQuery releases the hold on the $ shortcut identifier, so that other scripts can use it.
You can of course still use jQuery, simply by writing the full name instead of the shortcut.

Example
$.noConflict();
jQuery(document).ready(function() {
  jQuery("button").click(function() {
    jQuery("p").text("jQuery is still works");
  });
});

You can also create your own shortcut very easily.
The noConflict() method returns a reference to jQuery, that you can save in a  variable, for later use.
Example
var jq = $.noConflict();
jq(document).ready(function() {
  jq("button").click(function() {
    jq("p").text("jQuery still works");
  });
});

If you have a block of jQuery code which uses the $ shortcut and you do not want to change it all, you can pass the $ sign in as a parameter to the ready method. 
This allows you to access jQuery using $, inside this function - outside of it, you will have to use "jQuery".
Example
$.noConflict();
jQuery(document).ready(function($) {
  $("button").click(function() {
    $("p").text("jQuery still works");
  });
});

How to use AJAX with jQuery ?


AJAX (Asynchronous JavaScript and XML) is about loading data in the background and display it on the webpage, without reloading the whole page.
Examples of applications using AJAX : Gmail, Google Maps, Youtube, and Facebook tabs.

jQuery provides several methods for AJAX functionality.
With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page.


AJAX load()
Loads data from a server and puts the returned data into the selected element.

Syntax
$(selector).load(URL,data,callback);
data - A set of query string key/value pairs to send along with the request.
callback - Name of a function to be executed after the load() method is completed.

Example file : demo.txt
<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>
Loads the content of the file "demo.txt" into a specific <div> element.

Example
$("#div1").load("demo.txt");
Also, possible to add a jQuery selector to the URL parameter.

Example 
Loads the content of the element with id="p1", inside the file "demo.txt", into a specific div element
$("#div1").load("demo.txt #p1");
callback - A callback function to run when the load() method is completed.

The callback function can have different parameters :
responseTxt - contains the resulting content if the call succeed
statusTxt - contains the status of the call
xhr - contains the XMLHttpRequest object

Example
Displays an alert box after the load() method completes. If the load() method has succeed, it displays "External content loaded successfully!", and if it fails it displays an error message
$("button").click(function() {
  $("#div1").load("demo.txt", function(responseTxt, statusTxt, xhr) {
    if(statusTxt=="success")
      alert("External content loaded successfully!");
    if(statusTxt=="error")
      alert("Error: " + xhr.status + ": " + xhr.statusText);
  });
});


$.get()
Requests data from the server with an HTTP GET request.

Syntax
$.get(URL,callback);
callback - Name of a function to be executed if the request succeeds.

Example  Retrieve data from a file on the server
$("button").click(function() {
  $.get("demo.asp", function(data,status) {
    alert("Data: " + data + "\nStatus: " + status);
  });
});

The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.


$.post()

Requests data from the server using an HTTP POST request.

Syntax
$.post(URL,data,callback);
data - specifies some data to send along with the request.
callback - Name of a function to be executed if the request succeeds.

Example  Send some data along with the request
$("button").click(function() {
  $.post("demo_post.asp",
  {
    name:"Shaan",
    city:"New Delhi"
  },
  function(data,status) {
    alert("Data: " + data + "\nStatus: " + status);
  });
});

We pass in some data to send along with the request (name and city)
The ASP script in "demo_post.asp" reads the parameters, process them, and return a result.
The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.

How to work with dimensions of elements and browser window ?


width() and height()

Sets / returns the width or height of an element (includes NO padding, border or margin)
Example   Returns the width and height of a specified div element
$("button").click(function() {
  var txt="";
  txt += "Width: " + $("#div1").width() + "</br>";
  txt += "Height: " + $("#div1").height();
  $("#div1").html(txt);
});


innerWidth() and innerHeight()

Returns the width or height of an element (includes padding)
Example Returns the inner-width/height of a specified div element
$("button").click(function() {
  var txt="";
  txt+="Inner width: " + $("#div1").innerWidth() + "</br>";
  txt+="Inner height: " + $("#div1").innerHeight();
  $("#div1").html(txt);
});


outerWidth() and outerHeight()

Returns the width or height of an element (includes padding and border)
Example  Returns the outer-width/height of a specified div element
$("button").click(function() {
  var txt="";
  txt+="Outer width : " + $("#div1").outerWidth() + "</br>";
  txt+="Outer height : " " + $("#div1").outerHeight();
  $("#div1").html(txt);
});

outerWidth(true) and outerHeight(true)
Returns the width or height of an element (includes padding, border, and margin)
Example
$("button").click(function() {
  var txt="";
  txt+="Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
  txt+="Outer height (+margin): " + $("#div1").outerHeight(true);
  $("#div1").html(txt);
});


More examples of width() and height()
Example 1
Returns the width and height of the document (the HTML document) and window (the browser viewport)
$("button").click(function() {
  var txt="";
  txt+="Document width/height: " + $(document).width();
  txt+="x" + $(document).height() + "\n";
  txt+="Window width/height: " + $(window).width();
  txt+="x" + $(window).height();
  alert(txt);
});

Example 2  Sets the width and height of a specified div element
$("button").click(function() {
  $("#div1").width(500).height(500);
});