0

The following code works in Safari and Chrome, which is to say, it displays a copy of itself in the browser window:

<script src=https://code.jquery.com/jquery-2.1.3.min.js></script>
<script>
  function html_escape(s) { return $('<div/>').text(s).html(); }

  var url = document.location.href;
  $.get(url, function(data,status) {
    document.write('<pre>');
    document.write(html_escape(data));
    document.write('</pre>');
  });
</script>

But in Firefox it fails with the error "Not well formed", which I presume is because FF is trying to parse the contents of the file as XML. How can I stop FF from attempting to parse the file?

UPDATE: The problem only manifests itself when I run from a FILE: URL, so there is no MIME type. But that is in fact my use case.

Lisper
  • 223
  • 1
  • 6
  • I'm curious; why perform a `$.get()` request to the page you're already on? Why not just use `$('html')[0].outerHTML`? And have you checked that the [mime type is correct](http://stackoverflow.com/questions/677902/not-well-formed-error-in-firefox-when-loading-json-file-with-xmlhttprequest)? – Christian Apr 15 '15 at 06:49
  • Having the page request itself was just the simplest way I could think of to produce a self-contained example of the problem. And I only now realized that the problem only manifests itself when I run from a FILE: URL, so there is no MIME type. But that is in fact my use case. – Lisper Apr 15 '15 at 14:48

1 Answers1

0

It turns out that the default for the dataType parameter in a jquery AJAX request is an "intelligent guess". For some reason, in FireFox, this intelligent guess is different than Safari and Chrome, so you have to set the dataType explicitly to 'html' to make this work. So e.g. adding the following line:

  $.ajaxSetup({dataType: 'html'});

fixes the problem.

Lisper
  • 223
  • 1
  • 6