1

I am making somewhat of a content delivery network (CDN), but I want to have a jQuery AJAX call to a JavaScript file instead of an ASP file. The JavaScript file would then handle the input and then return a response. I don't want to use ASP, because I don't want to set up a whole server, especially because I am on a Mac. If this isn't possible that's fine, I'm just wondering.

(I can not provide any code, because I don't have enough that it would be useful.)

2
  • 4
    That makes little sense - where would the Javascript be executed, if not in the browser? And then you won't need an AJAX request in the first place. Commented Nov 23, 2013 at 20:44
  • It's not really a question that is easy to answer? A CDN is not something you make with javascript, and making an ajax call to get a javascript file is certainly doable, but why do you need it, and what are you expecting should happen when you get that javascript file etc. Commented Nov 23, 2013 at 20:45

2 Answers 2

3

If you want to use AJAX to retrieve a JavaScript file, that's kinda possible. You'll just need to dynamically add a <script> tag to the page (with the URL of the JS file in question) and the browser will take care of the rest. Don't forget you can add an onload handler to execute some code when the file's loaded.

If, however, you wanted to run JavaScript on your server, there's the excellent NodeJS for that. You will need to have another server running in this case.

Either way, there's not really a way to have code running outside of the browser and not have a separate server. Hope this helps, comment with any further questions.

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

Comments

2

I wonder if you're just trying to lazy load a JavaScript file from a URL, but that isn't your question, so I'll ignore that.

You can do just about anything, but that doesn't mean you should. You can't directly make an Ajax call to JavaScript. That's just a meaningless statement... The semantics of what you actually said would require an Ajax call to a server that has the ability to execute JavaScript code like a browser would, which can be done with a headless browser like PhantomJS. So, you would have to install PhantomJS on your server, Ajax to the server, have the server use PhantomJS to process some JavaScript code, then have the server respond to the Ajax call with whatever result you come up with.

I have to reiterate: You should not do this ^.

It would be like traveling the world to get to your neighbor's house. Anything that is possible within all of that mess, you could have just done it in the JavaScript code on the page or on the server in the server-side language.

1 Comment

Thank you, I knew it was insane, but I was just curious

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.