<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jQuery &#8211; Blackbams Blog</title>
	<atom:link href="https://blog.blackbam.at/category/web-development/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.blackbam.at</link>
	<description>development - digital arts - internet</description>
	<lastBuildDate>Tue, 08 Dec 2015 17:30:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>Ajax (jQuery) and loops: Local variable scoping and callback functions</title>
		<link>https://blog.blackbam.at/2015/12/08/ajax-jquery-and-loops-local-variable-scoping-and-callback-functions/</link>
					<comments>https://blog.blackbam.at/2015/12/08/ajax-jquery-and-loops-local-variable-scoping-and-callback-functions/#respond</comments>
		
		<dc:creator><![CDATA[Blackbam]]></dc:creator>
		<pubDate>Tue, 08 Dec 2015 17:30:15 +0000</pubDate>
				<category><![CDATA[JavaScript / Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[scoping]]></category>
		<category><![CDATA[variable]]></category>
		<guid isPermaLink="false">https://blog.blackbam.at/?p=2273</guid>

					<description><![CDATA[Thats a very annoying common problem I discovered &#8211; if calling Ajax within a Javascript loop the callback function will be called asynchronously. That means: All local variables in the callback function will have their future value instead the value they had when the loop was running. &#160; Example 1 (probably unwanted results): var types [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Thats a very annoying common problem I discovered &#8211; if calling Ajax within a Javascript loop the callback function will be called asynchronously. That means: All local variables in the callback function will have their future value instead the value they had when the loop was running.</p>
<p>&nbsp;</p>
<p><span style="color: #ff6666;">Example 1 (probably unwanted results):</span></p>
<pre lang="javascript">var types = ["boring","stupid","funny"];

for(key in types) {
    var type=types[key];

    $.ajax({url: "/?connection=get&amp;type="+type, success: function(result){
        $('#'+type).html(result);
    }
    });
}

</pre>
<p>Result: Only #funny is populated (with the results of the last callback, no matter which ends last).</p>
<p>&nbsp;</p>
<p><span style="color: #66ff66;">Example 2 (solution):</span></p>
<pre lang="javascript">
var types = ["boring","stupid","funny"];

for(key in types) {
    var type=types[key];

    (function(type) {
        $.ajax({url: "/?connection=get&amp;type="+type, success: function(result){
            $('#'+type).html(result);
        }
        });
    })(type);
}

</pre>
<p>Result: The elements with ids #boring, #stupid and #funny are populated with the result of the corresponding callback.</p>
<p>&nbsp;</p>
<p>Explanation: You have created a closure which is preserving the value of type according to the current iteration.</p>
<p><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.blackbam.at%2F2015%2F12%2F08%2Fajax-jquery-and-loops-local-variable-scoping-and-callback-functions%2F&#038;title=Ajax%20%28jQuery%29%20and%20loops%3A%20Local%20variable%20scoping%20and%20callback%20functions" data-a2a-url="https://blog.blackbam.at/2015/12/08/ajax-jquery-and-loops-local-variable-scoping-and-callback-functions/" data-a2a-title="Ajax (jQuery) and loops: Local variable scoping and callback functions"><img src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.blackbam.at/2015/12/08/ajax-jquery-and-loops-local-variable-scoping-and-callback-functions/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to scale images responsive with jQuery</title>
		<link>https://blog.blackbam.at/2014/02/15/how-to-scale-images-responsive-with-jquery/</link>
					<comments>https://blog.blackbam.at/2014/02/15/how-to-scale-images-responsive-with-jquery/#respond</comments>
		
		<dc:creator><![CDATA[Blackbam]]></dc:creator>
		<pubDate>Sat, 15 Feb 2014 02:35:38 +0000</pubDate>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://blog.blackbam.at/?p=2165</guid>

					<description><![CDATA[When coding responsive Web-Designs we usually have images in the content and in various containers with varying dimensions. In these containers, images should never be bigger than the HTML-container itself. In order to solve this problem I have developed this little script which may be helpful for some of you visitors, too. It is just [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>When coding responsive Web-Designs we usually have images in the content and in various containers with varying dimensions. In these containers, images should never be bigger than the HTML-container itself. In order to solve this problem I have developed this little script which may be helpful for some of you visitors, too. It is just required to set a jQuery selector for the container itself (to get the maximum width) and one to select all the images affected.</p>
<pre lang="javascript">

function scale_content_images() {

    $('#my_container img').each(function() {

            var the_image = this;
            var maxAvailableWidth = $('#my_container').width()-20; // Max width for the image
            var ratio = 0;  // Used for aspect ratio
            var width = $(this).width();    // Current image width
            var height = $(this).height();  // Current image height
            var orig_width = parseInt($(this).attr("width")); // the intended width of the image
            var orig_height = parseInt($(this).attr("height")); // the intended height of the image

            // if intended width/height not available, get original image dimensions and set these
            if(isNaN(orig_width)||isNaN(orig_height)) {
                var image = new Image();
                image.src = $(the_image).attr("src");
                orig_width = image.naturalWidth;
                orig_height= image.naturalHeight;
            }

            // Check if the current width is larger than the max
            if(orig_width>maxAvailableWidth) {
                ratio = maxAvailableWidth / width;   // get ratio for scaling image
                $(this).css("width", maxAvailableWidth); // Set new width
                $(this).css("height", height * ratio);  // Scale height based on ratio
                height = height * ratio;    // Reset height to match scaled image
                width = width * ratio;    // Reset width to match scaled image
            } else {
                $(this).css("width",orig_width);
                $(this).css("height",orig_height);
            }
        });
}

$(document).ready(function() {
      scale_content_images();
});

$(document).resize(function() {
      scale_content_images();
});

</pre>
<p><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.blackbam.at%2F2014%2F02%2F15%2Fhow-to-scale-images-responsive-with-jquery%2F&#038;title=How%20to%20scale%20images%20responsive%20with%20jQuery" data-a2a-url="https://blog.blackbam.at/2014/02/15/how-to-scale-images-responsive-with-jquery/" data-a2a-title="How to scale images responsive with jQuery"><img src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.blackbam.at/2014/02/15/how-to-scale-images-responsive-with-jquery/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to add social media buttons to jQuery Colorbox</title>
		<link>https://blog.blackbam.at/2013/07/02/how-to-add-social-media-buttons-to-jquery-colorbox/</link>
					<comments>https://blog.blackbam.at/2013/07/02/how-to-add-social-media-buttons-to-jquery-colorbox/#comments</comments>
		
		<dc:creator><![CDATA[Blackbam]]></dc:creator>
		<pubDate>Tue, 02 Jul 2013 13:02:03 +0000</pubDate>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Colorbox]]></category>
		<category><![CDATA[Social Media]]></category>
		<guid isPermaLink="false">https://blog.blackbam.at/?p=2059</guid>

					<description><![CDATA[Recently a client wanted to display social media buttons like facebook, twitter and google plus at the bottom of each image in jQuery Colorbox. As it is bad practice to change the source code of a Plugin, the following code worked well for this job. &#160; Note: This client wanted the full URL to be [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Recently a client wanted to display social media buttons like facebook, twitter and google plus at the bottom of each image in jQuery Colorbox. As it is bad practice to change the source code of a Plugin, the following code worked well for this job.</p>
<p>&nbsp;</p>
<p>Note: This client wanted the full URL to be liked, not the image itself &#8211; if you want to do so, you should &nbsp;like/recommend the attachemnt URL of the colorbox images, not the URL of the page. I will leave this up to you, to change the code in this way.</p>
<p>&nbsp;</p>
<div class="code_title">Add the following jQuery code to the footer of your page:</div>
<pre lang="javascript">

		jQuery(document).ready(function() { 
			var facebook = '<div class="cb_social_elem cb_fb"><div class="fb-like" data-href="" data-send="false" data-layout="button_count" data-width="160" data-show-faces="true" data-font="arial"></div></div>';
			var twitter = '<div class="cb_social_elem"><a href="https://twitter.com/share" class="twitter-share-button" data-lang="de" data-count="horizontal">Tweet</a></div>';
			var google = '<div class="cb_social_elem"><g:plusone size="medium" annotation="bubble"></g:plusone></div>';
			
		    jQuery("#cboxContent").append('<div id="cboxSocials">'+facebook+twitter+google+'</div><div class="clear"></div>'); 
		});
			
</pre>
<div class="code_title">CSS code</div>
<pre lang="css">
#cboxSocials {
	margin-top:-43px;
}

.cb_social_elem {
	float:left;
	margin-right:20px;
	width:120px;
}

</pre>
<p>&nbsp;</p>
<h3>Social Buttons Developer Links</h3>
<p><a href="https://developers.facebook.com/docs/reference/plugins/like/">Facebook Like</a></p>
<p><a href="https://dev.twitter.com/docs/tweet-button">Twitter</a></p>
<p><a href="https://developers.google.com/+/web/+1button/">Google+</a></p>
<p><a href="http://developer.linkedin.com/retrieving-share-counts-custom-buttons">LinkedIn</a></p>
<p><a href="http://business.pinterest.com/widget-builder/">Pinterest</a></p></p>
<p><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.blackbam.at%2F2013%2F07%2F02%2Fhow-to-add-social-media-buttons-to-jquery-colorbox%2F&#038;title=How%20to%20add%20social%20media%20buttons%20to%20jQuery%20Colorbox" data-a2a-url="https://blog.blackbam.at/2013/07/02/how-to-add-social-media-buttons-to-jquery-colorbox/" data-a2a-title="How to add social media buttons to jQuery Colorbox"><img src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.blackbam.at/2013/07/02/how-to-add-social-media-buttons-to-jquery-colorbox/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		
		
			</item>
	</channel>
</rss>
