<?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>Point47 &#187; JavaScript</title>
	<atom:link href="http://point47.com/journal/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://point47.com/journal</link>
	<description>less talk more code</description>
	<lastBuildDate>Tue, 08 May 2012 10:53:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Add jQuery Datepicker to a dynamic inserted input field via AJAX</title>
		<link>http://point47.com/journal/2012/03/add-jquery-datepicker-to-a-dynamic-inserted-input-field-via-ajax/</link>
		<comments>http://point47.com/journal/2012/03/add-jquery-datepicker-to-a-dynamic-inserted-input-field-via-ajax/#comments</comments>
		<pubDate>Wed, 28 Mar 2012 11:58:28 +0000</pubDate>
		<dc:creator>Sorin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://point47.com/journal/?p=203</guid>
		<description><![CDATA[Problem: You need to dynamically insert input fields in the DOM and attach to them the Datepicker component from jQuery UI. HTML PHP jQuery Pitfall: your input field must not have an id attribute or it must be unique. Preferable &#8230; <a href="http://point47.com/journal/2012/03/add-jquery-datepicker-to-a-dynamic-inserted-input-field-via-ajax/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Problem: You need to <strong>dynamically insert input fields in the DOM</strong> and attach to them the <a href="http://jqueryui.com/demos/datepicker/" title="Datepicker" target="_blank">Datepicker</a> component from <a href="http://jqueryui.com/" title="jQuery UI" target="_blank">jQuery UI</a>.<br />
<span id="more-203"></span></p>
<h3>HTML</h3>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;inputs_wrapper&quot;&gt;&lt;/div&gt;
&lt;a href=&quot;#&quot; id=&quot;addNewInput&quot;&gt;Add a new input and attach a Datepicker component&lt;/a&gt;
</pre>
<h3>PHP</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
echo '&lt;input type=&quot;text&quot; name=&quot;date&quot; class=&quot;datepicker&quot;&gt;';
?&gt;
</pre>
<h3>jQuery</h3>
<pre class="brush: jscript; title: ; notranslate">
// listen for click action
$('#addNewInput').click(function(e){
       e.preventDefault(); // blocks default link behavior (if it's a link element)

       // get the HTML with a AJAX request
       $.get(&quot;http://point47.com/examples/dynamicdatepicker/inputhtml.php&quot;, function(data){
            // adds the content in the #inputs_wrapper container and attaches the datepicker for each input found
            // also removes the class &quot;datepicker&quot; so we don't attach it again on the same inputs
            $('#inputs_wrapper').append(data).find('.datepicker').removeClass('datepicker').datepicker();
       });
   });
</pre>
<p><strong>Pitfall:</strong> your input field must not have an id attribute or it must be unique. Preferable don&#8217;t add an id attribute.</p>
]]></content:encoded>
			<wfw:commentRss>http://point47.com/journal/2012/03/add-jquery-datepicker-to-a-dynamic-inserted-input-field-via-ajax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CKEditor 3.0 Send and Receive data with AJAX</title>
		<link>http://point47.com/journal/2009/09/ckeditor-3-0-send-and-receive-data-with-ajax/</link>
		<comments>http://point47.com/journal/2009/09/ckeditor-3-0-send-and-receive-data-with-ajax/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 09:50:11 +0000</pubDate>
		<dc:creator>Sorin</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://point47.com/journal/?p=49</guid>
		<description><![CDATA[Attention: This is a quick tutorial and no in depth information will be provided. Use Google or whatever you want to learn more about PHP, Javascript and AJAX calls. Also on the CKEditor site you can find more info about &#8230; <a href="http://point47.com/journal/2009/09/ckeditor-3-0-send-and-receive-data-with-ajax/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>Attention: This is a quick tutorial and no in depth information will be provided. Use Google or whatever you want to learn more about PHP, Javascript and AJAX calls. Also on the CKEditor site you can find more info about the editor itself. </em></p>
<p>The old FCKEditor is now called CKEditor 3.0 and it&#8217;s:</p>
<blockquote><p>CKEditor is a text editor to be used inside web pages. It&#8217;s a WYSIWYG editor, which means that the text being edited on it looks as similar as possible to the results users have when publishing it. It brings to the web common editing features found on desktop editing applications like Microsoft Word and OpenOffice.</p></blockquote>
<p>You can download it from here: <a href="http://ckeditor.com">http://ckeditor.com</a>.</p>
<p>OK.. so I wanted to edit some HTML template files from an Admin area module and the new CKEditor looks pretty nice so I gave it a try.<br />
First I install the CKEditor and this is pretty easy to do, just download the editor and copy the files to the server.</p>
<p>Create a link to the editor:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;script type=&quot;text/javascript&quot; src=&quot;/ckeditor/ckeditor.js&quot;&gt;&lt;/script&gt;</div></td></tr></tbody></table></div>
<p>and then creat a text field area:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;textarea name=&quot;template_body&quot; id=&quot;template_body&quot; &gt;page content here&lt;/textarea&gt;</div></td></tr></tbody></table></div>
<p>and add the javascript code to load the editor:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;script type=&quot;text/javascript&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CKEDITOR.replace( 'template_body' );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;/script&gt;</div></td></tr></tbody></table></div>
<p>I added also a div to hold the received messages from the AJAX calls:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;div id=&quot;saveButton&quot; style=&quot;font-weight:bold&quot;&gt;&lt;/div&gt;</div></td></tr></tbody></table></div>
<p>Then I generated a tree/file view of all the template files(for sake of simplicity I will not show you how to do that).<br />
The links are generated like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;a href=&quot;&quot; onclick=&quot;getFile('test.html'); return false;&quot; id=&quot;file&quot; &gt;test.html&lt;/a&gt;</div></td></tr></tbody></table></div>
<p>The return false is so than I won&#8217;t need to use the stupid # on the href=&#8221;" so that the page won&#8217;t refresh.</p>
<p>And the two Javascript functions that make the Ajax calls are:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;script type=&quot;text/javascript&quot;&gt;<br />
function getFile(file)<br />
{ &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; var httpRequest = (!window.XMLHttpRequest)? new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;) : new XMLHttpRequest();<br />
&nbsp; &nbsp; httpRequest.onreadystatechange = function()<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(httpRequest.readyState == 4)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // send info to editor<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CKEDITOR.instances.template_body.setData(httpRequest.responseText);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('saveButton').innerHTML = '&lt;a href=&quot;&quot; onclick=&quot;saveFile(\'' + file + '\'); return false;&quot; &gt;SAVE TEMPLATE&lt;/a&gt;';<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; httpRequest.open(&quot;GET&quot;,&quot;/ajax/get_file.php?file=&quot;+file,true);<br />
&nbsp; &nbsp; httpRequest.send(null); &nbsp; &nbsp; &nbsp;<br />
}<br />
<br />
function saveFile(filename)<br />
{ &nbsp; &nbsp;<br />
&nbsp; &nbsp; // get info from editor<br />
&nbsp; &nbsp; var editordata = CKEDITOR.instances.template_body.getData();<br />
<br />
&nbsp; &nbsp; var httpRequest = (!window.XMLHttpRequest)? new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;) : new XMLHttpRequest();<br />
&nbsp; &nbsp; httpRequest.onreadystatechange = function()<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(httpRequest.readyState == 4)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(httpRequest.responseText == &quot;success&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('saveButton').innerHTML = '&lt;span style=&quot;color:red&quot;&gt;Template saved&lt;/span&gt;';<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('saveButton').innerHTML = httpRequest.responseText;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; var passingData = &quot;file=&quot;+filename+&quot;&amp;template_body=&quot;+ escape(editordata)<br />
&nbsp; &nbsp; httpRequest.open(&quot;POST&quot;, &quot;/ajax/save_file.php?&quot;+passingData, true);<br />
&nbsp; &nbsp; httpRequest.setRequestHeader(&quot;Content-type&quot;, &quot;application/x-www-form-urlencoded&quot;); &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; httpRequest.send(passingData);<br />
}<br />
&lt;/script&gt;</div></td></tr></tbody></table></div>
<p>The first function <strong>getFile()</strong>, gets the name of the file to load and send the response text from the PHP script to the editor.<br />
The PHP script gets the name of the file and with file_get_contents function I retrieve that template&#8217;s contents.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">echo file_get_contents('/templates/'.$_GET['file']);</div></td></tr></tbody></table></div>
<p>The editor shows the received data with the command:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CKEDITOR.instances.template_body.setData(httpRequest.responseText);</div></td></tr></tbody></table></div>
<p>&#8220;template_body&#8221; is the name of the text field that is replaced by the CKEditor.<br />
In this function I also add a new link to the page with innerHTML that makes the reference to the second function: <strong>saveFile()</strong>.</p>
<p>The second function <strong>saveFile()</strong> get the current name of the loaded template (test.html for example) and saves the data from the editor to<br />
a variable &#8220;editordata&#8221; with the call:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CKEDITOR.instances.template_body.getData();</div></td></tr></tbody></table></div>
<p>The editor data is sent to the PHP script, escaped and urlencoded with POST.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">// write content to file <br />
$filename = '/templates/'.$_POST['file'];<br />
$template_body = $_POST['template_body'];<br />
<br />
if (is_writable($filename)) // check file first<br />
{<br />
&nbsp; &nbsp; if (!$handle = fopen($filename, 'w')) &nbsp;// open file<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo 'fail open';<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; if (fwrite($handle, $template_body) === FALSE) // write file<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; echo 'fail write';<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; fclose($handle); // and close the file<br />
&nbsp; &nbsp; echo 'success';<br />
} <br />
else <br />
{<br />
&nbsp; &nbsp; echo 'fail not writable';<br />
}</div></td></tr></tbody></table></div>
<p>The PHP code is straight forward: open file, write contents to file, close file.<br />
If success is returned then with innerHTML we write that the template was saved if not the specific fail message will be shown.<br />
(usually those errors appear if the file is missing or the file path is wrong or the file permissions are wrong.)</p>
<p>So here you have it an browser based template editor.<br />
Oh and if your template has</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;html&gt;, &lt;body&gt;</div></td></tr></tbody></table></div>
<p>tags they will be deleted by the editor. Don&#8217;t know how to override that shit functionality.</p>
<p>Over and out.</p>
<p>Hey did you know about this?<br />
<a href="http://themeforest.net?ref=forapathy" title="wordpress themes, site templates, psd" target="_blank"><img src="http://envato.s3.amazonaws.com/referrer_adverts/tf_728x90_v5.gif" alt="wordpress themes, site templates" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://point47.com/journal/2009/09/ckeditor-3-0-send-and-receive-data-with-ajax/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Anti link/email spam script</title>
		<link>http://point47.com/journal/2009/03/anti-linkemail-spam-script/</link>
		<comments>http://point47.com/journal/2009/03/anti-linkemail-spam-script/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 10:24:44 +0000</pubDate>
		<dc:creator>Sorin</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://point47.com/journal/?p=38</guid>
		<description><![CDATA[123456&#60;script type='text/javascript'&#62; &#160; &#160;function nerf_email(){ &#160; &#160; &#160; return 'offi'.replace(/i/, 'ice') + '@' + 'point'.replace(/t/, 't47.com'); &#160; &#160;} &#160; &#160;document.write(nerf_email()); &#60;/script&#62; The above script will show: office @ point47.com]]></description>
			<content:encoded><![CDATA[<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;script type='text/javascript'&gt;<br />
&nbsp; &nbsp;function nerf_email(){<br />
&nbsp; &nbsp; &nbsp; return 'offi'.replace(/i/, 'ice') + '@' + 'point'.replace(/t/, 't47.com');<br />
&nbsp; &nbsp;}<br />
&nbsp; &nbsp;document.write(nerf_email());<br />
&lt;/script&gt;</div></td></tr></tbody></table></div>
<p>The above script will show: office @ point47.com</p>
]]></content:encoded>
			<wfw:commentRss>http://point47.com/journal/2009/03/anti-linkemail-spam-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

