Page 1 of 1

Jquery Pluggin code .

Posted: Thu Oct 30, 2014 7:56 am
by gautamz07
was following a small Jquery pluggin tutorial and heres the pluggin that was created :

Code: Select all

(function($){
	$.fn.targetblank = function(){

		// alert('Working');
		var targetArray = ['_blank' , '_self' , '_parent' , '_top'];
		var target = jQuery.trim($(this).attr('target'));

		if (target == undefined || target == '' || jQuery.inArray(target , targetArray) == false) {
			$(this).attr('target' , '_blank');
		}  // end of if statement .

	} // end of function
})(jQuery);


it gets called like this : 

<body>
 

<a href="http://www.sitepoint.com/how-to-develop-a-jquery-plugin/" >Sitepoint Article</a>


<script src="js/jquery.js"></script>
<script src="js/pluggin.js"></script>
<script>
    $(document).ready(function(){
      [color=#FF0000]  $('a').targetblank()[/color]
    });
</script>
</body>
i want to understand the Juery Pluggin code better :

my questions are :

what does this line mean :

Code: Select all

(function($){
2 . is the entire Jquery code a self - calling function ??

also ,

3. $.fn.targetblank = function(){

what does the above line really do ? or mean ??

Thanks .

Gautam .

Re: Jquery Pluggin code .

Posted: Thu Oct 30, 2014 8:25 am
by Celauran
gautamz07 wrote:what does this line mean :

Code: Select all

(function($){
It's a function definition that accepts $ as an argument.
gautamz07 wrote:2 . is the entire Jquery code a self - calling function ??
You're calling it after defining it, here

Code: Select all

(jQuery)
gautamz07 wrote:also ,

3. $.fn.targetblank = function(){

what does the above line really do ? or mean ??
You're defining a jQuery plugin so you can chain the method.

Code: Select all

$('a').targetblank()

Re: Jquery Pluggin code .

Posted: Fri Oct 31, 2014 12:10 am
by gautamz07
Thanks @Celauren but i still don't get this part .

Code: Select all

 $.fn.targetblank = function() 
:banghead: :banghead:

Whts that fn doing ????? why do all pluggins use that fn variable ??

Re: Jquery Pluggin code .

Posted: Fri Oct 31, 2014 7:07 am
by Celauran
Because jQuery.fn is shorthand for jQuery.prototype, so it makes your function available within the jQuery scope.

Re: Jquery Pluggin code .

Posted: Sun Nov 02, 2014 10:44 pm
by gautamz07
ok thanks @celauran .