Search the blog

You can’t actually read a HTML file in the browser using JavaScript as you would using Node’s fs. A browser has no access to a server’s file system; to read a HTML file you need to get it over HTTP. While this means an extra HTTP request, reading a HTML file with jQuery is really easy. Just use $.get(), which will read the HTML via AJAX and allow you to store it in a string.

Note that this will not work on files accessed via a different domain to the one you are calling your script from. Use JSONP for that.


    { '_': $.now() } // Prevents caching

).done(function(data) {

    // Here's the HTML
    var html = data;

}).fail(function(jqXHR, textStatus) {

    // Handle errors here

Tim Bennett is a Leeds-based web designer from Yorkshire. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.