jQuery code
/**
* Load uses XMLhttpRequests which does not support other charsets than UTF8
* UTF8 may be very useful and just as good but if you have a huge amount of data coded in another way
* the conversion may be very troubleful. In Sweden we use ISO-8859-1.
*
* This file changes all 'a' tags with the class ajax into new links
* which uses Cba to load content into the div with id content.
**
**
* So, What do we have here...
* Ajax, using CBA and jQuery, support for fancy links (bookmarkable, linkable) using
* hash(fackebook like #) or HTML 5's history.replaceState (depending on browser support) to create fancy links
*
* Uses modded CBA (cba.sf.net) instead of $.load since $.load uses xhr (xmlhttprequest) which has huge lack of charset support
* xhr also uses activeX in IE, which frankly sucks.
* xhr is browser dependable, older browsers/some cellphone browsers does not support it.
*
* Support for Server side scripting (php/ruby/python/perl/cf/asp(vb)/aspx/whatever)
* as well as Client side scripting (javascript)
*
* Almost fully cross browser. (lacks support for native back-forward buttons for IE<=6,
* We can acomplish it using an hidden iFrame but i don't think it's worth the weight, since there are'nt any advanced users using IE6- anyhow.
* If you want that support you might want to take a look at http://benalman.com/projects/jquery-hashchange-plugin/ Or
* http://tkyk.github.com/jquery-history-plugin/
*
*/
function loadcontent(toLoad){
if(toLoad=="" || toLoad=='/'){var toLoad='/content/start.php';}
// alert('loadcontent('+toLoad+') was triggered');
//$('#content:visible').fadeOut('normal',loadcontent2);
loadcontent2(); //Delete if effects are used.
function loadcontent2(){
//cbaUpdateElement('content','/loadcontent.php?page='+toLoad,0/* Loading? */,true/* Caching */,0 /*Template*/,0 /* Error Handling (jsfunc) */);
cbaUpdateElement('content','/loadcontent.php?page='+toLoad,0,false,0,0);
}
/**
*Fancy URLs in FF,Safari etc
**/
if(window.history.replaceState){
history.replaceState(null,document.title, window.location.hash.substr(1));
}
execute();
}
function fix_ready(){
//$('#content:hidden').fadeIn('normal');
linkfix();
return true;
}
function linkfix(){
$('a.ajax').click(function(e){
/**
* Workaround for opening in new window.
**/
if (e.shiftKey || e.ctrlKey || $(this).attr('click_function_applied')=='true'){
return true;
}else{
$(this).attr('click_function_applied','true');
var hash = window.location.hash.substr(1)
var toLoad = $(this).attr('href');
window.location.hash = $(this).attr('href');
/**
*Get content
**/
loadcontent(toLoad);
/**
*Store hash in created div for various browser usage
**/
var _historydiv = document.createElement('div');
_historydiv.setAttribute('id', 'historydiv');
_historydiv.setAttribute('name', hash);
document.body.appendChild(_historydiv);
/**
*Browser back button part 1
*Rest of browsers in the jungle
**/
var history=window.clearInterval(history);
if(document.getElementById('historydiv').name!="" && !window.history.replaceState &&
window.location.hash.substr(1)!="" && !("onhashchange" in window)){
var history=setInterval(function(){
if(document.getElementById('historydiv').name!=window.location.hash.substr(1)){
fallback();
}
},125);
}
/**
*Break
**/
return false;
}
});
}
$(document).ready(function(){
var hash = window.location.hash.substr(1);
var href = $('a.ajax').each(function(){
var href = $(this).attr('href');
if(hash==href){
var toLoad = hash;
// if(!toLoad || toLoad=='/'){var toLoad='/content/main.php';}
loadcontent(toLoad);
}
});
linkfix();
/**
*Back button FF,Chrome
*/
if(window.history.replaceState){
$(window).bind('popstate', function(event) {
var domain=window.location.hostname;
var url=window.location.href;
var rurl=url.replace(domain,'');
var rurl=rurl.replace('http://','');
if(rurl.indexOf('#')<1){
loadcontent(rurl);
}
});
}
/**
*Back button Internet Explorer
**/
if(!window.history.replaceState && ("onhashchange" in window)){
function locationHashChanged() {
if(document.getElementById('historydiv').name!=window.location.hash.substr(1)){
var toLoad = window.location.hash.substr(1);
loadcontent(toLoad);
}
}
window.onhashchange = locationHashChanged;
}
/**
*Rest of browsers in the jungle
* Back button Konqueror,Safari
**/
function fallback(){
if(document.getElementById('historydiv').name!=window.location.hash.substr(1)){
var toLoad = window.location.hash.substr(1);
loadcontent(toLoad);
}
}
});
/**
*This function is supposed to execute script within the CBA document
*/
function execute(){
$('#content script').each(function() {
var _script = document.createElement('script');
_script.type = 'text/javascript';
_script.setAttribute('async', 'true');
/**
*IEFIX (IE does not support .innerHTML on createElement)
**/
if(!_script.innerHTML){
_script.text=$(this).html().replace('\\\\',"\\");
}else{
_script.innerHTML = $(this).html().replace('\\\\',"\\");
}
document.body.appendChild(_script);
});
}