1

Is it possible to open a .js file in the browser window of IE8+ ? I have tried a direct URL to the file and I have also tried to write the file contents to a webpage using jquery's .load, and other methods such as XMLHttpRequest(), but in IE they all give me the Save/Open dialog.

In Firefox the direct link opens the file in the browser window and the jquery .load and the XMLHttpRequest() put the contents of the .js file right into a webpage. How can I get this behavior in IE8+ ?

MindMaster
  • 39
  • 7
  • Simplest answer: serve it with `Content-Type: text/plain`. Requires access to the server, obviously. :p – Amadan Jun 16 '12 at 00:59
  • The answer is here, grasshopper: http://stackoverflow.com/a/2492211/48082 – Cheeso Jun 16 '12 at 01:00
  • No! Don't do `Content-Type: text/plain`. Bad idea. – Cheeso Jun 16 '12 at 01:00
  • possible duplicate of [How can I convince IE to simply display application/json rather than offer to download it?](http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-do) – Cheeso Jun 16 '12 at 01:01
  • 2
    @Cheeso maybe? but, I am hoping for a solution that doesn't require the end users to edit their registry. They need to be able to access the file themselves, not just me. – MindMaster Jun 16 '12 at 01:04
  • well that's different. In that case maybe `text/plain` is the right thing. – Cheeso Jun 16 '12 at 01:12
  • @MindMaster - Well, it only happens in IE, so maybe you can use some VBScript there to help you out! – Derek 朕會功夫 Jun 16 '12 at 02:13
  • Or use PHP to serve the file as `text/plain`: `//www.example.com/plaintext.php?url=/blah.js` – Derek 朕會功夫 Jun 16 '12 at 02:17
  • I have tried using content-type, but I don't know what in the world I am doing with it! I found this answer from a different post: [How can I convince... ( one of the answers)](http://stackoverflow.com/a/9527826/881519). I tried implementing it like this: `req = new XMLHttpRequest(); if (req != undefined) { req.onreadystatechange = function() {ahahDone(url, target);}; req.setRequestHeader("contentType","text/plain; charset = utf-8"); req.open("GET", url, true); req.send(""); }` I works in IE just as it did before. Can someone help me out. – MindMaster Jun 17 '12 at 01:41

1 Answers1

0

If it is on the same domain as the calling page you can get the text with the responseText of an ajax call to the file. You can then put it in a readonly textarea or, if you turn ampersands and less-thans into entities, any html element.

kennebec
  • 102,654
  • 32
  • 106
  • 127
  • should have mentioned that the file is from a different domain and they have to be logged in the access the file. I am using a text area because I am also giving the users an option to copy/paste the file contents themselves for those who have browsers that will allow it. – MindMaster Jun 16 '12 at 20:26