Skip to content

Load Script an CSS #34

@bernd-reindl

Description

@bernd-reindl

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions