<?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; script</title>
	<atom:link href="http://kossovsky.net/index.php/tag/script/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>
	</channel>
</rss>

