0

I have this simple node.js server code which sends alert :

var sys = require("sys"), my_http = require("http"); my_http.createServer(function(request,response){ response.writeHeader(200, {"Content-Type": "application/javascript","Content-Length": "alert('');".length }); response.write("alert('');"); response.end(); }).listen(8080); sys.puts("Server Running on 8080"); 

The server is up and running :

enter image description here

and I do see the right headers for the response :

enter image description here

But the script is not executing !

Also , The console shows :

Resource interpreted as Document but transferred with MIME type application/javascript: "http://xxxx.com:8080/".

Question :

What am I missing and how can I make this alert work ?

4
  • But is that even possible? What's wrong with wrapping the script in <script> element? Commented Jan 21, 2014 at 17:44
  • sys? what ancient version of Node are you running? Commented Jan 21, 2014 at 19:26
  • @Joe Is that really matter ? I use expressJs but for the simplcity I used that example. Commented Jan 21, 2014 at 19:44
  • Well, given how much node has changed in the 3.5 years since sys was deprecated, I'd say it's relevant even if it's still the same behavior in something more recent. Commented Jan 21, 2014 at 19:56

2 Answers 2

3

For the script to execute by a browser, i'd rather use Content-Type:html & send

<script type="text/javascript">alert('test');</script> 

It's an old trick i used for "javascript streaming" before ajax existed.

For this to work, the browser has to know it is an an HTML document (or the "javascript url call" has to be initialiized from an exisiting HTML context)

Something like this:

<!DOCTYPE html> <html> <body> <iframe src="your_js_node_url_here"> </body> </html> 

The you can stream any javascript for your_js_node_url , keeping an "always open" connection .

Of course , this is a bit "old school", but enables sort of "code push" without websockets.

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

1 Comment

it doesnt work - i dont see the alert. (Property 'alert' of object [object Object] is not a function )
1

For the browser to execute your JavaScript there has to be something to tells it that this is executable code (as opposed to text to be formatted & displayed.)

You can send your JavaScript inside an HTML document and use the SCRIPT tag for this as in the following example:

var sys = require("sys"), my_http = require("http"); my_http.createServer(function(request,response){ response.writeHeader(200, {"Content-Type": "text/html", }); response.write("<html><body><script>alert('hello');</script></body>"); response.end(); }).listen(8080); sys.puts("Server Running on 8080"); 

Another approach is to fetch your JavaScript through another JavaScript program already on the browser (a-la AJAX) and have that other program execute it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.