Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether a linked resource loads successfully

Tags:

javascript

I'm using the following JavaScript to dynamically load stylesheets:

function set_stylesheet(name) {
    var link = document.getElementById('userstylelink');
    link.href = link.href.replace(/[^\/]+\.css$/, name + '.css');
}

Is there any way to determine whether the new CSS file is loaded successfully? If it fails, I'd like to be able to apply a default stylesheet.

like image 945
zildjohn01 Avatar asked Apr 09 '11 19:04

zildjohn01


1 Answers

You might want to see my answer to another similar question here: Detect and log when external JavaScript or CSS resources fail to load

Basically, you can add an onload callback to see if the file was loaded. (If you load via JS of course)

like image 112
joseeight Avatar answered Nov 26 '22 10:11

joseeight