<?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>.NET Tips and Tricks &#187; Maxim</title>
	<atom:link href="http://kossovsky.net/index.php/author/maksik/feed/" rel="self" type="application/rss+xml" />
	<link>http://kossovsky.net</link>
	<description>C# Code Snippets, ASP.NET Code Samples, .NET Tips and Tricks</description>
	<lastBuildDate>Sat, 25 Dec 2010 08:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Script as blocking element and dynamic script loading using document.write</title>
		<link>http://kossovsky.net/index.php/2009/07/script-as-blocking-element-and-dynamic-script-loading-using-document-write/</link>
		<comments>http://kossovsky.net/index.php/2009/07/script-as-blocking-element-and-dynamic-script-loading-using-document-write/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 11:44:20 +0000</pubDate>
		<dc:creator>Maxim</dc:creator>
				<category><![CDATA[Browser]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[blocking]]></category>
		<category><![CDATA[blocking elements]]></category>
		<category><![CDATA[div style]]></category>
		<category><![CDATA[document]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic element]]></category>
		<category><![CDATA[element content]]></category>
		<category><![CDATA[endless times]]></category>
		<category><![CDATA[flow]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[lt]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[script block]]></category>
		<category><![CDATA[script loading]]></category>
		<category><![CDATA[script type]]></category>
		<category><![CDATA[static element]]></category>
		<category><![CDATA[web page]]></category>
		<category><![CDATA[write]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=652</guid>
		<description><![CDATA[We all know that script is blocking element, meaning no content will be rendered untill script block is parsed and executed or downloaded then parsed and then executed in case of externale script. And we were told endless times that the best practice is to load scripts at the end of web page. It&#8217;s true. [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (3 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>We all know that <strong>script</strong> is blocking element, meaning no content will be rendered untill script block is parsed and executed or downloaded then parsed and then executed in case of externale script.</p>
<p>And we were told endless times that the best practice is to load scripts at the end of web page. It&#8217;s true. </p>
<p>We also have <strong>document.ready</strong> events thanks to numerous javascript frameworks that gives us opportunity to perform most of our JS actions when document is ready to be manipulated. But there are cases when you do need JS blocking features.<br />
<span id="more-652"></span><br />
Many ad serving systems work using <strong>document.write</strong> (One I&#8217;m have very good knowledge of is RealMedia), sometimes you need to load different versions of your scripts or your content rendering depends on some script and you have to be sure nothing is loaded untill your script is ready or may be you have script that depends on some script and you are not sure that this framework is loaded. Here comes <strong>document.write</strong> to your assistance.</p>
<p>I&#8217;m sure many people will say that I can load scripts using appendElement and then load depending scripts on it&#8217;s <strong>load</strong> event. This is also true, may be in my next post I will talk about this kind of script loading. But now lets see some samples.</p>
<p>Example #1</p>
<pre name="code" class="html">
	&lt;div style="border: 1px solid blue;width:200px;padding:10px;">
		Static element content
		&lt;script type="text/javascript">
			document.write("&lt;script type=\"text\/javascript\"
                                 src=\"writeContent.js\">&lt;\/script>");
		&lt;/script>
	&lt;/div>
</pre>
<p>Content of the writeContent.js file:</p>
<pre name="code" class="javascript">
document.write("&lt;div style=\"border:1px solid red;width:200px;\">
     Dynamic element content&lt;\/div>")
</pre>
<p>The result is pretty obvious:<br />
<img src="http://kossovsky.net/wp-content/uploads/2009/07/s1_1.gif" alt="Sample #1 - HTML Result" title="s1_1" width="238" height="82" class="size-full wp-image-659" /></p>
<p>In our next example we have some JS in in writeVariable.js file and we want to load and use it:</p>
<pre name="code" class="html">
&lt;div>
	&lt;script type="text/javascript">
		document.write("&lt;script type=\"text\/javascript\"
                      src=\"writeVariable.js\">&lt;\/script>");
		alert(myVar);
	&lt;/script>
&lt;/div>
</pre>
<p>Content of the writeContent.js file:</p>
<pre name="code" class="javascript">
    var myVar = "Some value";
</pre>
<p>You would expect to see alert  &#8220;Some value&#8221; but actually you will get &#8220;myVar is undefined&#8221; error. Why? Look at the HTML structure created and understand why:<br />
<img src="http://kossovsky.net/wp-content/uploads/2009/07/s2_1.gif" alt="Example #2 HTML structure" title="Example #2 HTML structure" width="202" height="48" class="alignnone size-full wp-image-665" /></p>
<p>Script block that you wrote created right after script block that was executed, so obviously at the time you tried to alert <strong>myVar</strong> it didn&#8217;t exist in the document.</p>
<p>The correct way to write it would be:</p>
<pre name="code" class="html">
	&lt;div&gt;
		&lt;script type="text/javascript"&gt;
                      document.write("&lt;script type=\"text/javascript\"
                           src=\"writeVariable.js\">&lt;\/script&gt;");
		&lt;/script&gt;
		&lt;script type="text/javascript"&gt;
                       alert(myVar);
		&lt;/script&gt;
	&lt;/div&gt;
</pre>
<p>You need to split your script into two blocks one writing the script and then second one using its data.</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (3 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fscript-as-blocking-element-and-dynamic-script-loading-using-document-write%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/script-as-blocking-element-and-dynamic-script-loading-using-document-write/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript boolean interpretation</title>
		<link>http://kossovsky.net/index.php/2009/07/javascript-boolean-interpretation/</link>
		<comments>http://kossovsky.net/index.php/2009/07/javascript-boolean-interpretation/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 08:16:27 +0000</pubDate>
		<dc:creator>Maxim</dc:creator>
				<category><![CDATA[Browser]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[about true]]></category>
		<category><![CDATA[alert message]]></category>
		<category><![CDATA[boolean interpretation]]></category>
		<category><![CDATA[boolean values]]></category>
		<category><![CDATA[document write]]></category>
		<category><![CDATA[eval]]></category>
		<category><![CDATA[javascript boolean]]></category>
		<category><![CDATA[javascript browser]]></category>
		<category><![CDATA[principle]]></category>
		<category><![CDATA[simple questions]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=613</guid>
		<description><![CDATA[How to deal with JavaScript boolean values, what will be evaluated as true and what as false !!!!.<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (5 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>I have two simple questions for you about boolean values in Javascript.<br />
When will  browser show alert message:</p>
<pre class="javascript" name="code">
        if (a-b) alert("it's true!");
</pre>
<p>And another one<br />
<span id="more-613"></span><br />
What will be printed as result of running this script:</p>
<pre class="javascript" name="code">
	for (var n=10; n; n-- )
	{
		document.write (n + ",");
	}
</pre>
<p>The answer for both questions relies on the same principle of what JavaScript engine will evaluate as true and what as false.</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (5 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fjavascript-boolean-interpretation%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/javascript-boolean-interpretation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Javascript pseudo protocol (is EVIL)</title>
		<link>http://kossovsky.net/index.php/2009/07/javascript-pseudo-protocol-is-evil/</link>
		<comments>http://kossovsky.net/index.php/2009/07/javascript-pseudo-protocol-is-evil/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 18:35:42 +0000</pubDate>
		<dc:creator>Maxim</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[href javascript]]></category>
		<category><![CDATA[javascript problems]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[script type]]></category>
		<category><![CDATA[span style]]></category>
		<category><![CDATA[style color]]></category>
		<category><![CDATA[style text]]></category>
		<category><![CDATA[text decoration]]></category>
		<category><![CDATA[text element]]></category>
		<category><![CDATA[text javascript]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[type javascript]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=586</guid>
		<description><![CDATA[This post is somehow related to my previous post about javascript function context.  Recently I helped one of my friends to debug a very funny problem. He told me that his inline event handler doen&#8217;t work, because he could not pass this object to his function. You probably wonder what did I find? &#60;script type="text/javascript"&#62; function [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (5 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>This post is somehow related to my previous post about <a href="http://kossovsky.net/index.php/2009/07/function-context-and-apply-function/" target="_blank">javascript function context</a>.  Recently I helped one of my friends to debug a very funny problem. He told me that his inline event handler doen&#8217;t work, because he could not pass <strong><em>this</em></strong> object to his function.<br />
You probably wonder what did I find? <img src='http://kossovsky.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
<span id="more-586"></span></p>
<pre class="javascript" name="code">    &lt;script type="text/javascript"&gt;
        function doSomething(obj) {
            obj.style.color = '#cc0000';
        }
    &lt;/script&gt;
    &lt;a href="javascript:doSomething(this)"&gt;Link&lt;/a&gt;</pre>
<p><strong><em>this</em></strong> keyword in his case was not A object as expected but <strong><em>window</em></strong>.<br />
Having javascript:  in href attribute is like running JavaScript from address bar of your browser. It will run in <strong><em>window</em></strong> context.<br />
Only if my friend was writing his code like this:</p>
<pre class="javascript" name="code">   ...
    &lt;a onclick="doSomething(this)"&gt;Link&lt;/a&gt;</pre>
<p>it would work like charm.</p>
<p>But I never use <strong><em>javascript:</em></strong> at all in my markup. Why?<br />
1. If you need text element that is clickable use span, any visual styling like text-decoration or cursor can be achieved using CSS.<br />
2. If you don&#8217;t have actual link behind you shouldn&#8217;t use A tag. As far as I know it&#8217;s not good for SEO.<br />
3. Javascript problems as i described above can arise<br />
I would write the code above like this:</p>
<pre class="javascript" name="code">    ...
    &lt;span style="text-decoration:underline;cursor:pointer;"
                 onclick="doSomething(this)"&gt;Link&lt;/span&gt;</pre>
<p>The only reason I know to use A for JavaScript  triggers is to achieve hover effect without use of JavaScript.<br />
If you know another reason why you should/shouldn&#8217;t use links as JavaScript triggers you&#8217;re welcome to comment.<br />
After this post you can get an impression that I don&#8217;t like javascript: notation and you will be wrong,  actually there are very usefull tricks I use all the time:<br />
1. Check some JS value on your page. Type javascript:alert(yourvariable) in the address bar and press enter.<br />
<img src="http://kossovsky.net/wp-content/uploads/2009/07/1.jpg" alt="" width="299" height="35" /></p>
<p>You will get :<br />
<img src="http://kossovsky.net/wp-content/uploads/2009/07/2.jpg" alt="" width="187" height="134" /></p>
<p>2. Or let&#8217;s say you have function somewhere on the page and you have to see it quick without searching through all your scripts. Or you wan&#8217;t to <span style="text-decoration: line-through">steal</span> see how someone implemented something <img src='http://kossovsky.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Just type  javascript:alert(yourdesiredfunction) and voila:<br />
<img src="http://kossovsky.net/wp-content/uploads/2009/07/3.jpg" alt="" width="283" height="44" /></p>
<p><img src="http://kossovsky.net/wp-content/uploads/2009/07/4.jpg" alt="" width="260" height="140" /></p>
<p>UPD:<br />
Just googled good page with JS best practices, it has Javascript: pseudoprotocol part that says almost exactly the same as my conclusions.<br />
<a href="http://www.javascripttoolbox.com/bestpractices/">http://www.javascripttoolbox.com/bestpractices/</a></p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (5 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fjavascript-pseudo-protocol-is-evil%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/javascript-pseudo-protocol-is-evil/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Javascript function context, &#8220;apply&#8221; method and lost &#8220;this&#8221; object</title>
		<link>http://kossovsky.net/index.php/2009/07/function-context-and-apply-function/</link>
		<comments>http://kossovsky.net/index.php/2009/07/function-context-and-apply-function/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 21:18:20 +0000</pubDate>
		<dc:creator>Maxim</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Callback]]></category>
		<category><![CDATA[fellow programmer]]></category>
		<category><![CDATA[javascript object]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[lame question]]></category>
		<category><![CDATA[request data]]></category>
		<category><![CDATA[script type]]></category>
		<category><![CDATA[server data]]></category>
		<category><![CDATA[solution number]]></category>
		<category><![CDATA[testthis]]></category>
		<category><![CDATA[text javascript]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=519</guid>
		<description><![CDATA[How many times you had problem or were asked about what happened to this object in your function? Or why this is not an object you expect it to be? Probably these times you have to remind yourself or your fellow programmer about function context. First of all I have to ask you all a [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.9" /></div><div>Rating: 4.9/<strong>5</strong> (8 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>How many times you had problem or were asked about what happened to <strong><em>this</em></strong> object in your function? Or why <strong><em>this</em></strong> is not an object you expect it to be? Probably these times you have to remind yourself or your fellow programmer about function context.<br />
First of all I have to ask you all a pretty lame question. If you write function in JS block right inside your HTML, what <strong><em>this</em></strong> keyword will refer to?</p>
<pre class="javascript" name="code">&lt;script type="text/javascript"&gt;
		function testThis(){
		  //who is "this"
		}
&lt;/script&gt;</pre>
<p>And now to the real example.<br />
<span id="more-519"></span><br />
Let’s say you build a simple Javascript object:</p>
<pre class="javascript" name="code">&lt;script type="text/javascript"&gt;
		var mathObj = {
			pi: 3.14,
			getCircumference : function(data) {
				alert(this.pi * 2 * data.radius);
			}
		}

		mathObj.getCircumference(10);
&lt;/script&gt;</pre>
<p>You have properties, you have functions, you can use <strong><em>this</em></strong> keyword to access properties of the object from its functions. Everyone is happy.<br />
Now we want to add function that will get some server data using AJAX and run one of the functions our object has.<br />
(To all who wondered what is $ stands for &#8211; I use JQuery framework in all my JS samples)</p>
<pre class="javascript" name="code">&lt;script type="text/javascript"&gt;
		var mathObj = {
			pi: 3.14,
			getCircumference : function(data) {
				alert(this.pi * 2 * data.radius);
			},
			getRemoteCircumference: function(url) {
				$.getJSON(url, this.getCircumference);
			}
		}

		mathObj.getRemoteCircumference("RadiusData.js");
&lt;/script&gt;</pre>
<p>If we have {&#8220;radius&#8221;:5} in RadiusData.js file you would expect the result of getCircumference would be 31.4 but you will get NaN. Why is that? Because <strong><em>this</em></strong> keyword in this case will refer to some kind of object with request data returned by JQuery. What can you do?<br />
Solution number one is to use your object name:</p>
<pre class="javascript" name="code">...
	getCircumference : function(data) {
		alert(mathObj.pi * 2 * data.radius);
	}
...</pre>
<p>Ugly. You don&#8217;t have to return on object name in every function.<br />
Solution number two is to use anonymous function and explicitly call your function:</p>
<pre class="javascript" name="code">
....
	getRemoteCircumference: function(url) {
		$.getJSON(url, function(data){
			mathObj.getCircumference(data);
		});
	}
...
</pre>
<p>Good enough solution, but again I don&#8217;t like reusing object name&#8230;<br />
And now to my favorite solution.</p>
<pre class="javascript" name="code">&lt;script type="text/javascript"&gt;
   function getContextFunc(context, func) {
       return function() {
           func.apply(context, arguments);
       };
   }

   var mathObj = {
       pi: 3.14,
       getCircumference: function(data) {
           alert(this.pi * 2 * data.radius);
       },
       getRemoteCircumference: function(url) {
          $.getJSON(url, getContextFunc(this, this.getCircumference));
       }
   }

   mathObj.getRemoteCircumference("RadiusData.js");
&lt;/script&gt;</pre>
<p>Here apply function will come in handy. <strong><em>getContextFunc</em></strong> returns anonymous function that will run our original method in given context.<br />
<a target="_blank" href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/apply">See some documentation</a></p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.9" /></div><div>Rating: 4.9/<strong>5</strong> (8 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Ffunction-context-and-apply-function%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/function-context-and-apply-function/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

