1

I know this is a stupid question.

But just out of curiosity, is there a way to post an html file(a .html file) as a js/css file (renamed as .js or .css) with the type header as either HTML or js/css and get the data at the browser end in normal html format? I mean the browser reading it as an html itself and display it as any other html page?

1 Answer 1

6

What matters for the browser when displaying a resource is the Content-Type HTTP header. As long as this header is set by the web server to text/html, the extension doesn't really matter. The resource will be interpreted as HTML. So you could configure the web server to serve .js file with a text/html Content-Type and it will work.

The file doesn't even need to exist on the server. That's why I prefer to talk about resources instead of files. You could use a server side language and configure it to respond to some virtual address (with the .js extension) and serving HTML content with the text/html Content-Type header.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, the Content-Type is all that matters.
Example: cs.tut.fi/~jkorpela/odd/hello.js (Has been done using AddType text/html;charset=utf-8 JS in the .htaccess file of the directory, when using an Apache server.)
IE is known to violate Internet protocols regarding content type in some situations, overriding the Content-Type header when it thinks it knows better. But this probably won’t matter here; it mostly makes IE treat a document as text/html on the basis of the actual contents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.