09.04.2009
In a chat the concept of private variables in a javascript object came up. I had seen it before but never really had done them before so I thought I would play around with it a bit.
Javascript by default does not have private variables. If you have variables that need to be ‘read-only’ or ‘private’ you can limit it’s visibility with a closure inside your constructor function.
-
-
-
var foo = function(a) {
-
var privateVar = a;
-
this.getPrivate = function() {
-
return a;
-
}
-
this.setPrivate = function(newvar) {
-
a = newvar;
-
}
-
}
-
-
var bar = new foo(1000);
-
alert(bar.getPrivate()); // Alerts 1000
-
bar.setPrivate(‘Hello’); // Sets privateVar to ‘Hello’
-
alert(bar.getPrivate()); // Alerts ‘Hello’
-
alert(bar.privateVar); // Alerts ‘undefined’
-
-
Posted in Javascript No Comments »
09.04.2009
This tutorial is intended to be a first look at the concept of “Ajaxifying” your website. That is, creating a progressive-enhancement to how users navigate from page to page. Traditionally when a user clicks a link, the new page in it’s entirety will be loaded. If your header / navigation / footer stays the same throughout, there is little need to re-download that content, furthermore to re-render that content after it downloaded.
This is a generic pattern that you can expand on and implement with your web application.
First the Javascript
-
-
var QuickLink = Class.create({
-
-
currentPage : null,
-
-
initialize : function() {
-
this.attachLinkEvents();
-
//Set an interval to check for changes in the URI
-
setInterval(function(){
-
this.refresh();
-
}.bind(this), 1000);
-
},
-
-
refresh : function() {
-
//If a change has taken place, call the dispatch function
-
var url = window.location.hash.split(‘#’);
-
if (url.length > 1) {
-
this.dispatch(url[1]);
-
}
-
},
-
-
attachLinkEvents : function() {
-
//Gather a list of links, and replace the default action
-
//With our javascript action
-
var links = $$(‘a’);
-
links.each(function(linkElement, index) {
-
var href = linkElement.href;
-
if(href.indexOf(‘http://www.yourdomain.com/’) != -1) {
-
var uri = href.split(‘http://www.yourdomain.com’)[1];
-
Event.observe(linkElement, ‘click’, function(e){
-
window.location.hash = uri;
-
//Disallow the default click to change pages
-
Event.stop(e);
-
});
-
}
-
-
});
-
},
-
-
dispatch : function(url) {
-
//Check to see if the page you are linking to is NOT the current URL
-
//If different, then send your request
-
if (this.currentPage != url) {
-
this.currentPage = url;
-
var myAjax = new Ajax.Updater(‘content’, url, {
-
parameters : { ‘quick’ : true }
-
});
-
}
-
-
-
}
-
});
-
-
//initialize
-
-
new QuickLink();
-
-
Your template
-
-
<? if ($_POST[‘quick’]!=true) { ?>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
-
<title>Page Title</title>
-
</head>
-
<body>
-
<div id="content">
-
<? } ?>
-
Some content here
-
<? if ($_POST[‘quick’]!=true) { ?>
-
</div>
-
</body>
-
</html>
-
<? } ?>
-
In conclusion
What we are doing is using AJAX just to refresh the content area. Your template checks to see if it was an AJAX request and if so, does not send the header and footer of your content back. This saves you in the following areas:
- Does not have to re-download everything
- If you have scripts that need to be initialized, the scripts will not need to be reparsed
Moving further
If using a more advanced setup like an MVC framework or if your app has different scripts or elements depending on the page I suggest on the server side you instead send back a payload including dependent scripts and maybe flags to highlight elements as well. Then you can use output buffering to store the HTML chunk in part of the payload. Return the response as a JSON object have your Javascript put things where they need to go and THEN write the innerHTML of the content part of the payload.
Posted in Ajax, Performance No Comments »
09.04.2009
There are bigger solutions to this problem that you may need to consider. But if you have a medium trafficked site this will work.
-
-
<FilesMatch "\\.(js|css|html|htm|php|xml)$">
-
SetOutputFilter DEFLATE
-
</FilesMatch>
-
A little something for Expires headers & ETags too
-
-
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
-
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
-
Header unset ETag
-
FileETag None
-
</FilesMatch>
-
Posted in Performance, Web No Comments »