<?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>Thijs Lensselink&#039;s Blog &#187; Javascript</title>
	<atom:link href="http://lenss.nl/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://lenss.nl</link>
	<description>Webdevelopment and stuff...</description>
	<lastBuildDate>Thu, 26 Apr 2012 21:48:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Drag &amp; drop Uploads with XMLHttpRequest2 and PHP Revised</title>
		<link>http://lenss.nl/2010/09/drag-drop-uploads-with-xmlhttprequest2-and-php-revised/</link>
		<comments>http://lenss.nl/2010/09/drag-drop-uploads-with-xmlhttprequest2-and-php-revised/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 12:26:42 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[firefox 3.6]]></category>
		<category><![CDATA[XMLHttpRequest2]]></category>

		<guid isPermaLink="false">http://lenss.nl/?p=786</guid>
		<description><![CDATA[A while back i wrote a post where i explained how to implement the new XMLHttpRequest2 object. The main point of the post was to use sendAsBinary() so we can stream file uploads from the client to the server. The post i made showed some code snippets to make this possible. And the Javascript part [...]]]></description>
			<content:encoded><![CDATA[<p>A while back i wrote a <a href="http://lenss.nl/2010/01/drag-drop-uploads-with-xmlhttprequest2-and-php/">post </a> where i explained how to implement the new XMLHttpRequest2 object. The main point of the post was to use <a href="https://developer.mozilla.org/en/XMLHttpRequest">sendAsBinary()</a> so we can stream file uploads from the client to the server.</p>
<p>The post i made showed some code snippets to make this possible. And the Javascript part is all fine and dandy. But last week i had an interesting mail conversation with <a href="http://braincracking.org/">Jean-Pierre Vincent</a> about the memory consumption on the server side of things.</p>
<p>The result of some testing revealed the server would need at least the amount of memory equal to the file size. For small files this is no problem. But with bigger files this becomes a problem. Although i couldn&#8217;t reproduce Jean-Pierre&#8217;s results. I wasn&#8217;t very happy with the test results.</p>
<blockquote><p>
Upload 2.8 MB file results in 3.1 MB memory usage<br />
Upload 29 MB file results in 30 MB memory usage
</p></blockquote>
<p>A bit more testing revealed that <a href="http://php.net/manual/function.file-put-contents.php">file_put_contents()</a> was the culprit. Which seems logical if you think about it. It reads the file into memory and dumps it again. Not very elegant for big files. Besides we are trying to stream files. So why should we read them completely in to memory? We shouldn&#8217;t :)</p>
<p>Jean-Pierre decided to go with the DataForm object to solve the issue. I still want to look into that. But have not found any information about. Besides that i knew the problem was on the server side. So i rewrote the receive() method to be less memory intensive. The results are considerably better then before.</p>
<blockquote><p>
Upload 2.8 MB file results in 0.4 MB memory usage<br />
Upload 29 MB file results in 0.4 MB memory usage
</p></blockquote>
<p>The memory usage was measured with <a href="http://php.net/manual/function.memory-get-peak-usage.php">memory_get_peak_usage()</a>. And the new code is posted below:</p>
<pre name="code" class="php">
public function receive()
{
    if (!$this->isValid()) {
        throw new Exception('No file uploaded!');
    }

    $fileReader = fopen('php://input', "r");
    $fileWriter = fopen($this->_destination . $this->_fileName, "w+");

    while(true) {
        $buffer = fgets($fileReader, 4096);
        if (strlen($buffer) == 0) {
            fclose($fileReader);
            fclose($fileWriter);
            return true;
        }

        fwrite($fileWriter, $buffer);
    }

    return false;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2010/09/drag-drop-uploads-with-xmlhttprequest2-and-php-revised/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Drag &amp; drop Uploads with XMLHttpRequest2 and PHP</title>
		<link>http://lenss.nl/2010/01/drag-drop-uploads-with-xmlhttprequest2-and-php/</link>
		<comments>http://lenss.nl/2010/01/drag-drop-uploads-with-xmlhttprequest2-and-php/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 23:02:38 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[firefox 3.6]]></category>
		<category><![CDATA[XMLHttpRequest2]]></category>

		<guid isPermaLink="false">http://lenss.nl/?p=653</guid>
		<description><![CDATA[I finally had some time to read through my ever growing list must read items and play with some new software. While reading up on the new Firefox 3.6 i noticed it came with the new XMLHttpRequest [2] object based on the new file API. And according to the new specs. This would allow for [...]]]></description>
			<content:encoded><![CDATA[<p>I finally had some time to read through my ever growing list must read items and play with some new software. While reading up on the new Firefox 3.6 i noticed it came with the new <a href="http://www.ideal.nl/">XMLHttpRequest</a> [<a href="http://www.w3.org/TR/XMLHttpRequest2/">2</a>] object based on the new <a href="http://dev.w3.org/2006/webapi/FileAPI/">file API</a>. And according to the new <a href="http://dev.w3.org/2006/webapi/FileAPI/">specs</a>. This would allow for easy file uploads. Now there&#8217;s been some <a href="http://hacks.mozilla.org/2009/12/uploading-files-with-xmlhttprequest/">examples</a> [<a href="http://arstechnica.com/open-source/news/2009/11/w3c-publishes-draft-of-new-file-api-spec.ars">2</a>] on the web already. But i just wanted to get my hands dirty.</p>
<p>The new XMLHttpRequest object makes is possible to send files in a few different formats. The most important being the binary format. The code for sending a request with XMLHttpRequest2 looks the same as the previous version. Except for sendAsBinary() in this case.</p>
<pre name="code" class="javascript">
var xhr = new XMLHttpRequest();

fileUpload = xhr.upload,
fileUpload.onload = function() {
    console.log("Sent!");
}

xhr.open("POST", "upload.php", true);
xhr.sendAsBinary(file.getAsBinary());
</pre>
<p>So let&#8217;s set things up for drag &#038; drop. We need a div that will be the main drop point. And we need some event listeners to catch the drag * drop events. Let start by creating the drop zone. For this we use two simple divs. The outer div will listen for the drag &#038; drop events. And the inner will catch the files.</p>
<pre name="code" class="html">
<div id="container">
<div id="drop"></div>
</div>
</pre>
<p>Now let&#8217;s create our upload code.</p>
<pre name="code" class="javascript">
var upload = {
    setup : function() {},
    uploadFiles : function() {event}
}
window.addEventListener("load", upload.setup, false);
</pre>
<p>The setup method will set all event listeners for drag &#038; drop. And register the upload handler.</p>
<pre name="code" class="javascript">
var container = document.getElementById('container');
var drop = document.getElementById('drop');

container.addEventListener("dragenter", function(event) {
        drop.innerHTML = '';
        event.stopPropagation();
        event.preventDefault();
    },
    false
);

container.addEventListener("dragover", function(event) {
        event.stopPropagation();
	event.preventDefault();
    },
    false
);

container.addEventListener("drop", upload.uploadFiles, false);
</pre>
<p>As you could see above. the uploadFiles() method gets a event returned from the drag &#038; drop action. This is where the new <a href="http://dev.w3.org/2006/webapi/FileAPI/">file APi</a> comes in play. To get to the file property we access the dataTransfer object.</p>
<pre name="code" class="javascript">
var files = event.dataTransfer.files;
</pre>
<p>The actual uploading is easy as cake.</p>
<pre name="code" class="javascript">
for (var x = 0; x < files.length; x++) {

    var file = files.item(x);
    var xhr = new XMLHttpRequest();

     fileUpload = xhr.upload,
     fileUpload.onload = function() {
         console.log("Sent!");
    }

    xhr.open("POST", "upload.php", true);

    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    xhr.setRequestHeader("X-File-Name", file.fileName);
    xhr.setRequestHeader("X-File-Size", file.fileSize);
    xhr.setRequestHeader("Content-Type", "multipart/form-data");

    xhr.sendAsBinary(file.getAsBinary());
}
</pre>
<p>That's it for the client side. There is however a small problem on the receiving side. When handling uploaded files in PHP we expect the <strong>$_FILES</strong> array to be populated. This is not the case when streaming files from the client to the server. To get the needed file information we set some headers on the client side <strong>X-File-Name</strong> and <strong>X-File-Size</strong>. And since the <strong>$_FILES</strong> are is empty. We need an other way to get the file contents. So we will use php://input streams for that.</p>
<p>The contents of upload.php look like this:</p>
<pre name="code" class="php">
require_once('Streamer.php');

$ft = new File_Streamer();
$ft->setDestination('data/');
$ft->receive();
</pre>
<p>With setDestination() the destination path for the uploaded files is set. And recieve() listens for any incoming files. Most of the magic is done in the recieve() method. So here's the code.</p>
<pre name="code" class="php">
public function receive()
{
    if (!$this->isValid()) {
        throw new Exception('No file uploaded!');
    }

    file_put_contents(
        $this->_destination . $this->_fileName,
        file_get_contents("php://input")
    );

    return true;
}
</pre>
<p>I am impressed! This promises a lot of good. And offers some interesting options. Let's hope all browsers implement this gem. I still have one issue though. I can't get this to work in firefox under linux. The drag &#038; drop events do not seem to function properly with files being dragged from the desktop. anybody know why? </p>
<p>If you interested in the complete code. you can find it <a href="http://lenss.nl/code/dragDropUpload.tar.gz">here</a></p>
<p><strong>**Small update**</strong><br />
<a href="http://lenss.nl/2010/09/drag-drop-uploads-with-xmlhttprequest2-and-php-revised/">http://lenss.nl/2010/09/drag-drop-uploads-with-xmlhttprequest2-and-php-revised/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2010/01/drag-drop-uploads-with-xmlhttprequest2-and-php/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Animated loader on form submit</title>
		<link>http://lenss.nl/2009/03/animated-loader-on-form-submit/</link>
		<comments>http://lenss.nl/2009/03/animated-loader-on-form-submit/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 16:03:15 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[preloader]]></category>

		<guid isPermaLink="false">http://lenss.nl/?p=387</guid>
		<description><![CDATA[In one of our sites we load content of a third party travel organization. And some of these calls can take some time. But when the pages are loading nothing really happens to the browser state. This is confusing for the users of the website. So i decided to add some animated preloader images. Just [...]]]></description>
			<content:encoded><![CDATA[<p>In one of our sites we load content of a third party travel organization. And some of these calls can take some time. But when the pages are loading nothing really happens to the browser state. This is confusing for the users of the website. So i decided to add some animated preloader images. Just to show the users the site is still doing stuff.</p>
<p>This however confronted me with a small problem. The preloader images are supposed to show up when a form is submitted. And this is exactly what&#8217;s causing the problem. In my first attempt i created the image object when the form was submitted. On submit a JavaScript function was called. This function created the image object and displayed it.</p>
<pre name="code" class="javascript">
function loader() {
	var loaderImg = new Image();
        loaderImg.src = 'some/image/url/';
}
</pre>
<p>This however resulted in an image displayed that had no active animation. This happens i think because the image is not fully loaded when the user hits submit on the form. So to fix this i had to load the images before the page was completely rendered.</p>
<pre name="code" class="javascript">
var loaderImg = new Image();

window.onload = function() {
        loaderImg.src = 'some/image/url';
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2009/03/animated-loader-on-form-submit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mootools FX.Slide Flicker bug II</title>
		<link>http://lenss.nl/2008/06/mootools-fxslide-flicker-bug-ii/</link>
		<comments>http://lenss.nl/2008/06/mootools-fxslide-flicker-bug-ii/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 18:39:19 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Mootools]]></category>

		<guid isPermaLink="false">http://we.designandco.de/?p=13</guid>
		<description><![CDATA[Today i got a question from Richard. About the flicker bug and how to implement the fix. The problem that he had with Javascript throwing an illegal character error was because of copy pasting from this blog. I&#8217;m not sure why the single quotes get screwed up. But i will have a look at it. [...]]]></description>
			<content:encoded><![CDATA[<p>Today i got a question from <a href="http://www.usablenet.co.uk/" target="_blank">Richard</a>. About the flicker bug and how to implement the fix. The problem that he had with Javascript throwing an illegal character error was because of copy pasting from this blog. I&#8217;m not sure why the single quotes get screwed up. But i will have a look at it. I also didn&#8217;t state which version of <a href="http://mootools.net/" target="_blank">mootools</a> i was using. It was 1.1 at the time. And i was using the full uncompressed version.</p>
<p>But since then version 1.2 of <a href="http://mootools.net/" target="_blank">mootools</a> came out. And id didn&#8217;t really had time yet to look into it. But this version also has the same annoying bug. So i looked at the code. The <a href="http://docs.mootools.net/Plugins/Fx.Slide" target="_blank">FX.Slide</a> class changed a bit. And the core and  plugins are split up into separate files now. But the fix remains the same. And since i don&#8217;t really want to write a whole article about it right now. I decided to create two sets of archives. One with the fix for version 1.1 and one for version 1.2. The changes i made are explained in the previous article. But for 1.1 they are around line 4453 and 4458. For 1.2 if you only use the FX.Slide plugin it&#8217;s around line 17 and 29.</p>
<p>You can download the archives here:<br />
<a href="http://we.designandco.de/code/mootools.1.1.slide.flickerbug.tar.gz" target="_blank">version 1.1</a><br />
<a href="http://we.designandco.de/code/mootools.1.2.slide.flickerbug.tar.gz" target="_blank">version 1.2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2008/06/mootools-fxslide-flicker-bug-ii/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Mootools FX.Slide Flicker bug</title>
		<link>http://lenss.nl/2008/06/mootools-fxslide-flicker-bug/</link>
		<comments>http://lenss.nl/2008/06/mootools-fxslide-flicker-bug/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 12:43:19 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Mootools]]></category>

		<guid isPermaLink="false">http://we.designandco.de/?p=5</guid>
		<description><![CDATA[When my coworker decided to use mootools for a project we are working on. I decided to give it a try also. I decided to use the FX.Slide effect on some tab based boxes. This was pretty straight forward. The mootools library is pretty easy in usage. And the documentation is darn good. Bellow i [...]]]></description>
			<content:encoded><![CDATA[<p>When my coworker decided to use <a href="http://mootools.net/" target="_blank">mootools</a> for a project we are working on. I decided to give it a try also.</p>
<p>I decided to use the <a href="http://docs.mootools.net/Plugins/Fx.Slide" target="_blank">FX.Slide</a> effect on some tab based boxes. This was pretty straight forward. The mootools library is pretty easy in usage. And the documentation is darn good. Bellow i will show how to add the most basic FX.Slide effect. This is directly copied from the mootools <a href="http://docs.mootools.net/" target="_blank">manual</a>.</p>
<p>To create the sliding effect all you need is the following lines of javascript.</p>
<pre class="javascript">window.addEvent('domready', function() {

    // create an instance of the FX.Slide object
    var mySlide = new Fx.Slide('test');

    // Attach the click event to the wrapper div
    $('toggle').addEvent('click', function(e) {
        e = new Event(e);
	mySlide.toggle();
	e.stop();
    });
});</pre>
<p>And of course some HTML to represent the sliding elements.</p>
<pre class="html">&lt;h3 class="section"&gt;Fx.Slide Vertical&lt;/h3&gt;

&lt;a id="toggle" href="#"&gt;toggle&lt;/a&gt;
&lt;div id="test"&gt;
foo
&lt;/div&gt;</pre>
<p>And some CSS to cover it all in a nice sauce.</p>
<pre class="css">#test {
    background: #222;
    color: #fff;
    padding: 10px;
    margin: 20px;
    border: 10px solid pink;
}</pre>
<p>So this looks great. It functions well. And the sliding is very smooth. There is one small problem though. When the wrapper contains an element with a style attribute overflow set to &#8220;auto&#8221;. The sliding will cause a flicker effect on the page. This is probably caused by rescaling the wrapper. Witch in it&#8217;s turn rescales the element with attribute overflow set to auto.</p>
<p>I didn&#8217;t really try to figure out why this happens. Maybe that&#8217;s a later project. But i did come up with a small fix. When a slide object is created. FX.Slide wraps a div around the container that will slide in and out. This wrapper has the attribute overflow set to hidden. I couldn&#8217;t figure out why overflow is set to hidden on this wrapper. So i added the ability to set it through the options object. When overflow is set to auto the flicker bug disappears. To add this feature we need to add some small changes to the FX.Slide class.</p>
<pre class="javascript">options: {
	mode: 'vertical'
},</pre>
<p>First we change the code above to reflect the code below:</p>
<pre class="javascript">options: {
	mode: 'vertical',
	overflow: 'hidden'
},</pre>
<p>Then on the third line of the initialize function we change:</p>
<pre class="javascript">this.wrapper = new Element('div', {'styles': $extend(this.element.getStyles('margin'), {'overflow': "hidden"})}).injectAfter(this.element).adopt(this.element);</pre>
<p>To:</p>
<pre class="javascript">this.wrapper = new Element('div', {'styles': $extend(this.element.getStyles('margin'), {'overflow': this.options.overflow})}).injectAfter(this.element).adopt(this.element);</pre>
<p>When all this is done and we want to create a FX.Slide object without the flicker bug. We add the options parameter. The constructor will make sure the right value is used.</p>
<pre class="javascript">var mySlide2 = new Fx.Slide('test', {overflow: 'auto'});

$('toggle').addEvent('click', function(e){
    e = new Event(e);
    mySlide2.toggle();
    e.stop();
});</pre>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2008/06/mootools-fxslide-flicker-bug/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Missing Gem for webdesign</title>
		<link>http://lenss.nl/2008/06/missing-gem-for-webdesign/</link>
		<comments>http://lenss.nl/2008/06/missing-gem-for-webdesign/#comments</comments>
		<pubDate>Sun, 01 Jun 2008 09:35:46 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://we.designandco.de/?p=3</guid>
		<description><![CDATA[Lately i&#8217;ve been working on a project. And one of the requirements is that the user interface works from IE 6 and up. Now i have to admit that it&#8217;s been a while since i did CSS. And forgot what a pain it is to get things working cross browser. There is always those little [...]]]></description>
			<content:encoded><![CDATA[<p>Lately i&#8217;ve been working on a project. And one of the requirements is that the user interface works from IE 6 and up. Now i have to admit that it&#8217;s been a while since i did <a href="http://www.w3.org/Style/CSS/" target="_blank">CSS</a>. And forgot what a pain it is to get things working cross browser. There is always those little irritating IE 6 bugs that double your working time because you either need to fix a bug. Or because you need to create a different stylesheet for a certain browser.</p>
<p>So when i hit the <a href="http://www.positioniseverything.net/explorer/threepxtest.html" target="_blank">3 pixel gap</a> bug in IE 6 again. I wanted to start and create a javascript based fix. But first did a google search. To see of nobody had the same genius idea. And low and behold. Somebody did. I found this great little Gem ie7-js It&#8217;s a Javascript library created by <a title="Dean Edwards" href="http://dean.edwards.name/" target="_blank">Dean Edwards</a>. For a lot of things their already were fixes floating around the web. But this has it all. It fixes the <abbr title="Portable Network Graphics (format)">PNG</abbr> alpha transparency bug in IE 5.5 and 6.0. Makes sure the :hover element is not only usable on &lt;a&gt; elements. And a whole lot more. The new IE8-js version is even more complete. And adds advanced CSS selectors and properties.</p>
<p>Using it is as simple as including this in the header section of your (X)HTML:</p>
<pre class="prettyprint"><a id="IE8.js">&lt;!--[if lt IE 8]&gt;
&lt;script src="IE8.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;![endif]--&gt;
</a></pre>
<p>*demo <a href="http://ie7-js.googlecode.com/svn/test/index.html">http://ie7-js.googlecode.com/svn/test/index.html</a></p>
<p>*site <a href="http://code.google.com/p/ie7-js/" target="_blank">http://code.google.com/p/ie7-js/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2008/06/missing-gem-for-webdesign/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
