Embed HTML Code in JavaScript File

by Max Rohde,

Problem

You would like to include HTML code as a String in a JavaScript file.

Solution

Firstly, load the HTML code into a JavaScript variable (e.g. by using jQuery.ajax()).

$.ajax({url: 'http://mydomain.com/htmlfile.html'})

.done(function(html) {

Then apply the following two simple regular expressions on the html code to generate valid JavaScript code.

var safeHtml = html

  .replace(/\n/g, '\\n')

  .replace(/\"/g, '\\"');

You can use this String now to build a JavaScript file:

var myScript = 'var html="' + safeHtml + '";';
Categories: javascript