web developing
21 Apr
Everybody knows that sometimes you need header and footer for almost every page. In Zend Framework you don’t need to include them in every template page, as it will be if you were using Smarty for instance. You just need to use Zend_Layout. It’s easy and it’s helpful.
Well if you have to have a given controller action with no use of Zend_Layout, you just need to disable it.
… simply by placing this line in you controller action:
$this->_helper->layout->disableLayout();
15 Apr
Well usually the URL is formed like so – http://example.com/index.php#anchor1, if you have anchors, of course. And the problems is simple to define. With PHP you cannot grab the anchor part of the link (that anchor1 in the example). That’s of course normal behaviour, the anchors are used by the client (your browser usually) to position the page at a specific location, and being a client side technique is simple to guess that PHP cannot access them.
Today the web is more and more full of such an Ajax rich applications, almost every one of them don’t refresh the page and use anchors to save the path of the user, and that’s how they manage the bookmarks of a specific state in the page. We often need this technique, cause every other change in the URL, but the anchors, invoke a reload of the page, which is bad. But however why don’t we use directly this anchors from the Ajax application? That’s because we’d like to speed up things. We load the page with an anchor in the URL, like so:
http://example.com/index.php#load-me-first
but in that case the page should load, than the JavaScript should grab the anchor, and than to process via Ajax request the data from the server … and even that is not enough, cause than the data should be placed on the right place in the page.
Note: In fact, most of the fully flash based sites work on the same schema.
We need the initial load to happen directly with PHP which is much more faster. But the problem is that, as I said PHP cannot read the anchor of the link. If you have http://example.com/index.php#load-me-first, none of the _SERVER variables will return the anchor, because as I noticed that’s client side issue.
Well we need to combine the client and server side techniques to solve the problem. There comes JavaScript combined with PHP and all this is made with the help of the Cookies.
Let see the situation. If we have an anchor in the URL we can easily grab this with JavaScript:
<script>
var query = location.href.split(‘#’);
</script>
Now the query[1] has the value of the anchor part of the URL. Now you can set this in a cookie.
<script>
var query = location.href.split(‘#’);
document.cookies = ‘anchor=’ + query[1];
<script>
Note: it’s important to setup the cookie to be persistent only for this session so next time you enter the site there should not be such an existent one.
So far so good, what we need more is just to read that cookie in PHP, so you can put this PHP code above.
<script>
var query = location.href.split(‘#’);
document.cookies = ‘anchor=’ + query[1];
<script>
<?php
echo $_COOKIE['anchor'];
?>
Of course, yes. This is not working correctly. In fact it’s working correctly from the second load on, but on the initial load of the page the _COOKIE array does not has any anchor key inside. That’s because this part of the code is executed before the browser setup the cookie on the client.
Well there is a workaround. You simply should wait for the cookie to come from the server. Like so:
<script>
var query = location.href.split(‘#’);
document.cookies = ‘anchor=’ + query[1];
<?php if (!$_COOKIE['anchor']) : ?>
window.location.reload();
<?php endif; ?>
<?php
echo $_COOKIE['anchor'];
?>
Now everything is OK. You can parse the _COOKIE['anchor'].
It’s exteamly important to put the <script> tag as high as you can, so the browser will setup and check for the cookie before anything is rendered to the page. In that case the reload of the page is so quick so you will not experience any delay at all.
14 Apr
Well it may look strange to want to do that exaclty. Who wants to have a <a> tag in another. It really looks semanticaly incorrect, but however, every normal A grade browser’s displaing it correctly, except .. IE6.
The Microsoft team may be too much semanticaly involved in that problem, I should guess, but that’s really impossible.
The workaround is trivial. You just put the <a> tags one after another and adjust them with relative position and margin with negative values.
<a href=”#”>link here</a>
<a href=”#” style=”margin:-10px 0 0; position : relative;”>link there</a>
14 Apr
Well let’s assume you’ve a <div> and like to load a .swf flash file into it. We suppose you’re using swfobject or any other library or even if you don’t use library that should be a simple thing to do.
But what happens if the <div> has the style property “display:none”. Than unfortunately the flash file does not load. You get the file from the server, but when you set the <div> to be “display:block”-ed the flash starts the play from the very beggining.
Actually that’s supposed to be default action. In fact the workaround is like so. You put the <div>, not to be hidden with that “display:none”, but to be with 0px width and height. something like that:
<div style=”width:0px;height:0px;overflow:hidden;”></div>
The overflow is important – cause you’d like to load the swf file into the <div> and if the swfobject.crateSWF or embedSWF is with 100% width and height, the flash will result with zero height and zero width.
So you may put the flash to load in something like that:
<div style=”width:800px;height:600px;”></div>
and put that one into the div with overflow hidden. Than the outer <div> will be ivisible but present to the page and the inner one will be with fixed width and height.
Now when the flash loads, or if the user clicks on some part of the page or fires some event, you can simply reset the width and height of the outer <div>. The resulting piece of code will look like:
<div style=”width:800px;height:600px;overflow:hidden;”>
<div style=”width:800px;height:600px;”> the flash goes here with 100% width and height </div>
</div>
With jQuery that should look as follows:
$(document).ready(function() {
$(‘#outer-div’).css({ width: ‘800px’, height : ‘600px’ });
});
Now you can load the flash in background, even you can start loading it after everything else is loaded – i.e. on document ready event of jQuery, so if you have havy computations in that flash you can simple show something else to the user till the flash loads.
7 Apr
In a series of articles I’ll share what’s my experience with image optimization. Of course the most images you have the most page weight you have. That’s why optimizing your images should be your primary task.
First, I’m gonna mention several formats, which everyone of the webdevs are using widely. There are GIF, JPEG and PNG, where PNG can be either PNG8 and PNG24 (PNG32 is also available as name of that format).
What are the differences between them and when to use one or another?
Well some of us just use one or another even without knowing the difference or without thinking about the optimization. I remember when I just put the images in JPEG with no idea what are the other formats before.
In fact all the formats has pros and cons.
The JPEG format can be either progressive or baseline. The difference is that the progressive JPEG can be loaded directly in the browser with very low quality and then to load with better quality. Some experts insist that this is old school, but I don’t think so. When the image is not the required one (think of a wallpaper preview) the user can skip and go to another without waiting the hole image. The baseline JPEG is loading the good quality image but starting from top to bottom. With portions of the image.
Of course the Internet Explorer 6 don’t support the progressive downloading of an JPEG even if it’s progressive. Well this is no issue till the IE loads the image like a baseline JPEG.
You can certainly make some test to determine which JPEG is progressive and which is baseline, from the loading behaviour in your browser. If you’d like to convert an image to a progressive JPEG or baseline you can use a free open source programs out there in the wild online space. Once they are converted to one or another format you can use them widely. Because most of the tools are comand line open source tools you can automate them on the server where the user uploads it’s images. Now every big image can be converted to be progressive.
Well the tests show that for images small than 10 K best compression you get with baseline, and for bigger images the small size you get is with progressive JPEGs. There’s the rule use the baseline for thumbs and progressive for any other big image.
This format is well known from the early ages of the web. It allows transparency and animation. Actually the problem with the GIF images is that they don’t support semi-transparency which is a problem nowadays. Today the web users are used to good quality images and somehow the GIF is not suitable. But it’s still the most used format for icons and of course animations.
Well as I’ll mention in a while the PNG8 is a better format than GIF for transparent small icons. But if you should use some kind of an animation like ajax loading circle (or bar) the GIF format is suitable for you. Than you must use a GIF.
As I said PNG can be either PNG8 or PNG24 (which is also called PNG32). Actually PNG8 acts just like GIF, with the important exception that it supports semi-transparency, but yet again on every browser except IE6. In IE6 the PNG8 is just the same like GIF which is not so bad. If you’d like to use GIF for an icon, you should make it PNG8. There are semi-transparent pixels (except IE6 and above) and you get better experience on other browsers than GIF.
In fact PNG32 is not transparent on IE6 (on transparent pixels IE puts some kind of gray pixels). There you can use AlphaImageLoader filter, but that can dramaticaly slow down your browser, so it’s not a good idea.
You should avoid the PNG32 transparent images and the usage of AlphaImageLoader filter.
For every of these formats there lots of tools that remove system information from the image crash and crunch the files so that they become smaller. They can be automated and used on the server.
Now when you use different formats you can be aware of the differences between them and to use the appropriate ones.