Jquery Pluggin code .

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Jquery Pluggin code .

Post 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 .
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Jquery Pluggin code .

Post 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()
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Jquery Pluggin code .

Post 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 ??
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Jquery Pluggin code .

Post by Celauran »

Because jQuery.fn is shorthand for jQuery.prototype, so it makes your function available within the jQuery scope.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Jquery Pluggin code .

Post by gautamz07 »

ok thanks @celauran .
Post Reply