beezee / djax

Dynamic pjax, responds to layout changes across requested pages
http://beezee.github.com/djax.html
171 stars 27 forks source link

Load Script an CSS #34

Open bernd-reindl opened 10 years ago

bernd-reindl commented 10 years ago

Hello.

I have found your jQuery Plugin and would use it in a TYPO3 Website. My problem is, when i starts with the startpage and go to the contactform, the needed CSS and JS files are not loaded. So i wrote some code to load this files dynamically.

Maybe this code is interesting for you.

[code]

window.includes = { script: {}, css: {}};

function includesRemember() { jQuery(document).find('script').each(function(index, element) { var src = $(element).attr('src'); window.includes.script[src] = 1 }); jQuery(document).find('link[rel="stylesheet"]').each(function(index, element) { var href = $(element).attr('href'); var media = $(element).attr('media'); window.includes.css[href+'-'+media] = 1 });
}

function includesUpdate(data) { jQuery(data.response).filter('script').each(function(index, element) { var src = $(element).attr('src'); if(!window.includes.script[src]) { includesAppendScript(src); window.includes.script[src] = 1 } }); jQuery(data.response).filter('link[rel="stylesheet"]').each(function(index, element) { var href = $(element).attr('href'); var media = $(element).attr('media'); if(!window.includes.css[href+'-'+media]) { includesAppendCss(href, media); window.includes.css[href+'-'+media] = 1 } });
}

function includesAppendScript(filename){ var fileref=document.createElement('script') fileref.setAttribute("type","text/javascript") fileref.setAttribute("src", filename) document.getElementsByTagName("head")[0].appendChild(fileref) }

function includesAppendCss(filename, media){ var fileref=document.createElement("link") fileref.setAttribute("rel", "stylesheet") fileref.setAttribute("type", "text/css") fileref.setAttribute("media", media) fileref.setAttribute("href", filename) document.getElementsByTagName("head")[0].appendChild(fileref) }

$(window).bind('djaxLoad', function(e, data) { includesUpdate(data); } [/code]

includesRemember is called after djax is inited.

Best regards Reindl Bernd