Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JavaScript set the mime type on window.open

I am trying to open a debug window within Javascript. Javascript will pass the debug window a JSON string which JSONView (a Chrome extension) should display in a nicely formatted way.

For this to work the MIME type must be "application/json". Is it possible to send the mime type and JSON string to window.open as a parameter some how? I think the MIME type and content has to be set on window.open otherwise JSONView won't get triggered.

I did try this, but it did not work:

var x = window.open("about:blank", 'x'); 
var doc = x.document; 
doc.open("application/json"); 
doc.write($(".trend_chart").attr("data-trendChart"))
like image 670
Vanson Samuel Avatar asked Oct 10 '11 02:10

Vanson Samuel


People also ask

Who sets the MIME type?

During the ObjectType step in the request handling process, the server determines the MIME type attributes of the resource requested by the client. Several different server application functions (SAFs) can be used to determine the MIME type, but the most commonly used one is type-by-extension.

What is window open JavaScript?

open() The open() method of the Window interface loads a specified resource into a new or existing browsing context (that is, a tab, a window, or an iframe) under a specified name.


Video Answer


2 Answers

The document that your opening should be of type "application/json" you cannot send it as a parameter in the window.open method since it's out of context. The browser instead is the one that determines the file type using the request headers.

window.open("http://www.yoursite.com/file.json", "mywindow");

You should see the json file within JSONView without problems. If the browser still asks you to download the file, your installation of JSONView is probably broken.

like image 124
Michael D. Irizarry Avatar answered Sep 28 '22 09:09

Michael D. Irizarry


It is not possible.

You'd be better off doing:

console.log(JSON.parse($(".trend_chart").attr("data-trendChart")));
like image 37
Domenic Avatar answered Sep 28 '22 09:09

Domenic