I always did:
$('body').append('<link rel="stylesheet" href="/template/foo.css" type="text/css" />');
instead of head
Ah ... sorry, I just realized what your problem is. One strategy is to extract the path from the script from the DOM itself:
$('script').each(function(i,el){ var path = el.src.match(/^(.+)\/foo.js$/); if (path) { $('body').append('<link rel="stylesheet" ' + 'href="' + path[1] + '/foo.css" ' + 'type="text/css" />' ); } })
source share