Questions tagged [javascript]

JavaScript (not to be confused with Java) is a dynamic, weakly-typed language used for client-side as well as server-side scripting. Use this tag for questions regarding ECMAScript and its various dialects/implementations (excluding ActionScript and Google-Apps-Script). Unless another tag for a framework/library is also included, a pure JavaScript answer is expected.

JavaScript is a dynamic, object-based, duck-typed, prototype-based, weakly typed language traditionally used for client-side scripting in web browsers. can also be run outside of the browser with the use of a framework like , , , or . Despite the name, it is unrelated to the Java programming language and shares only superficial similarities.

Unless a tag for a framework or library is also included, a pure JavaScript answer is expected for questions with the tag.

JavaScript runs on nearly every Operating System, and an engine is included in mainstream web browsers. Developed in 1995 by Brendan Eich at Netscape Communications, it was originally called LiveScript but was renamed to JavaScript due to Netscape's friendly relationship with Sun Microsystems at the time.

Standalone JavaScript engines or interpreters are available as well, including:

  • Mozilla's , the first JavaScript engine ever written, currently used in Mozilla Firefox.
  • , Google's JavaScript interpreter, used in Google Chrome.
  • , a cloud-based / server-side interpreter that provides programmatic "macro-like" control of Google Apps services and documents.
  • , built on top of , a platform which enables server-side applications to be written in JavaScript.
  • Windows includes , a JavaScript variant in Windows Script Host.
  • Mozilla also offers , an implementation of JavaScript built in Java, typically embedded into Java applications to provide scripting to end users.
  • (except for the Chromium project) implements the engine.
  • (originally derived from HyperTalk) is now an ECMAScript dialect and uses a lot of ECMAScript APIs.
  • Embeddable, portable ECMAScript engine in C with small memory footprint.
  • a framework, IDE, and Server built on supporting server-side JavaScript
  • A Meteor application is a mix of client-side JavaScript that runs inside a web browser or PhoneGap mobile app, server-side JavaScript that runs on the Meteor server inside a Node.js container. (according to MeteorJS docs )

The Mozilla Developer Network contains high-quality documentation on JavaScript.

JavaScript is typically used to manipulate the Document Object Model (DOM) and Cascading Style Sheets (CSS) within the browser. This allows user interface scripting, animation, automation, client-side validation, and much more.

With the recent emergence of platforms such as Node.js, JavaScript can now be used to write server-side applications. In addition, it is also used in environments that aren't web-based, like PDF documents, site-specific browsers, desktop widgets etc.

Nomenclature

Although it was developed under the name Mocha, the language was officially called LiveScript when it first shipped in beta releases of Netscape Navigator 2.0 in September 1995, but it was renamed JavaScript when it was deployed in the Netscape browser version 2.0B3.

The change of name from LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. The final choice of name caused confusion, giving the impression that the language was a spin-off of the Java programming language, and the choice has been characterized as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new web programming language.

People often use the term JavaScript informally. The language and the term originated from Netscape. ECMAScript, JavaScript, and JScript are terms that are easy to confuse.

ECMAScript was developed as a standardization of Netscape's JavaScript and Microsoft's independently-developed JScript. The canonical reference is the ECMAScript® 2015 Language Specification. While JavaScript and JScript aim to be compatible with ECMAScript, they also provide additional features (and other deviations) not described in the ECMA specifications. Other implementations of ECMAScript also exist.

The differences today for those who use JavaScript are negligible; people generally do not distinguish the JavaScript and JScript variations from ECMAScript.

ECMAScript versions

Most modern browsers implement JavaScript based on the specification, although some fail to implement some ES6 features. However, older browsers such as Internet Explorer 8 implement the ECMAScript 3 specification, which does not contain functions such as Function.prototype.bind and even JSON.parse, amongst others.

The current version of ECMAScript is version 7 (), properly known as ECMAScript 2016, which was finalized in June 2016.


When asking a JavaScript question, you should:

  1. Debug your JavaScript code (see Creativebloq, MDN, Google, & MSDN).
  2. Isolate the problematic code and reproduce it in a Stackoverflow code snippet or an external online environment such as JSFiddle or JS Bin (remember to also include the code in the question itself).
  3. If a library or framework is used, then tag the question with the appropriate tags: for jQuery, for Prototype, for MooTools, and so on. However, if a framework is not used or necessary, do not include these tags.
  4. Mention which browser the code is having problems on, and what error messages, if any, were thrown by the browser (use the Developer Tools for your browser (see "Useful Tools" below) to see these messages). If the question is browser-specific, use tags , , , , , ,etc.
  5. Only tag the question as or if you are asking about an issue that concerns the combination of one of those with JavaScript and could only be answered with information specifically regarding either of those subjects.

Learning JavaScript

Security

JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the Web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform Web-related actions, not general-purpose programming tasks like creating files. Second, scripts are constrained by the same origin policy: scripts from one Web site do not have access to information such as usernames, passwords, or cookies sent to another site. Most JavaScript-related security bugs are breaches of either the same origin policy or the sandbox.

Content Security Policy is the main intended method of ensuring that only trusted code is executed on a Web page.

Useful Tools

Interactive JavaScript learning

Wisdom from the Stack

Useful links

Free JavaScript Programming Books

Videos


Example JavaScript code

This script displays "Hello World" on your screen.

window.onload = function() {
   alert('Hello World!');
}

Demo!


Frequently Asked Questions

Find some answers to some of the more frequently asked questions about JavaScript and related technology below.

Q: I have this JSON structure, how can I access property x.y.z?
A: http://stackoverflow.com/q/11922383

Q: I'm adding events in a for loop but all handlers do the same thing, why?
A: http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example

Q: I want to compare something against multiple values, is there an easy way to do it?
A: http://stackoverflow.com/questions/13737091/concise-way-to-compare-against-multiple-values

Q: How to set up proper inheritance?
A: http://stackoverflow.com/questions/11072556/objects-dont-inherit-prototyped-functions/11072626

Q: How do JavaScript closures work?
A: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work/

Q: Why does setTimeout() inside a for loop always use the latest value?
A: http://stackoverflow.com/questions/5226285

Q: How to return the response from an AJAX call from a function?
A: http://stackoverflow.com/questions/14220321

Q: Why don't my handlers hooked up in a loop work correctly, and what can I do about it?
A: http://stackoverflow.com/questions/16794707/javascript-generate-dynamically-handler/16794762#16794762

Q: How can I get query string values?
A: http://stackoverflow.com/questions/901115

Q: What does “use strict” do in JavaScript?
A: http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it

Q: How can I make a redirect page in jQuery/JavaScript?
A: http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript

Q: How to sort an array of objects by a property value?
A: http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript

Q: I'm adding elements with JavaScript or jQuery at a later point and adding events but they're not firing, why?
A: You might want event delegation.

Q: How can I only keep items of an array that match a certain condition?
A: http://stackoverflow.com/questions/27131984/how-can-i-only-keep-items-of-an-array-that-match-a-certain-condition

Q: How can I debug my JavaScript code?
A: http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code

More information:

Chat Room

189 questions
0
votes
1 answer

Can't run server-side Javascript on Classic ASP

I'm trying to run a server side piece of JavaScript code on my ASP site. The whole site is using VBScript, but this piece is written in JavaScript, so I'm using a separate tag, rather than setting the language at the top of the page. This works…
ulu
  • 233
  • 5
  • 10
0
votes
1 answer

How can I accept responses from an API with multiple subdomains?

I write website prototypes that use data from a server I do not control, the response is in JSON and the server does not support JSONP, thus I have problems with CORS as I am developing in the client only. I have managed to get around this by…
Blaise
  • 3
  • 1
0
votes
1 answer

Heroku redirect file to serve from CDN

My Heroku backed app is now serving a certain JS file, but due to high rpms I would like to serve it from my CDN. Is it possible to make a redirect from myapp.herokuapp.com/file.js to mycdn.com/bucket/file.js without makeing the redirect inside my…
maephisto
  • 131
  • 4
0
votes
1 answer

Apache Tomcat + Apache2 + mod_proxy: Issues regarding cluster

I have a Tomcat 7.0.57 cluster setup behind an Apache2 using mod_proxy. Setup: Apache2: 192.168.2.139 Tomcat Node1: ajp://192.168.2.166:8010 (http connector also defined on port 8082) Tomcat Node2: ajp://192.168.2.166:8011 (http connector also…
Mosh Pit
  • 339
  • 2
  • 4
  • 13
0
votes
2 answers

modrewrite for css / jss minified

I have a web site hosted in a CentOS 5-Plesk-Apache server. I have recently added a second site to the server for serve dynamic content. I have established rewrite rules for images (static content) that works pretty well. The module rewrites the…
jab
  • 51
  • 4
0
votes
1 answer

Nginx redirection to url writen in requested file

I need to setup dynamic redirections. For example, a user open a link : http://www.example.com/2/3/4/ and see the page on the following address: http://www.example2.pl/something. I know about RewriteMap, but I need something which don't require…
Thomas
  • 3
  • 1
0
votes
0 answers

HTML5 media loading sometimes suspends or aborts: misconfigured Apache?

Recently, some code that has been working fine for months started to run unexpectedly. That code is just a media files loading JavaScript function, that uses jQuery. It's pretty long, but in essence it is like this: var…
Joan Botella
  • 101
  • 2
0
votes
1 answer

Overriding a JS file from server from proxy

I have an Apache reverse-proxy server set up between a client machine and a server. What I want to achieve is to override a javascript file sent from the server with the one from the proxy before sending the file to the client. I don't want to…
Wai Yan
  • 103
  • 4
0
votes
0 answers

How to approach SSL encryption with file uploads

I added SSL to a site I'm putting together, and it caused a huge error that I have no idea how to approach. The user logs into the site, which allows them to upload files to the server into a directory based on their username. Adding SSL didn't…
0
votes
1 answer

Apache [negotiation:error] while opening javascript file

I have javascript file with name circle.terminals.cmd.var.js and apache return 500 error with: [Sun Jun 15 15:00:55.194900 2014] [negotiation:error] [pid 20095] [client 127.0.0.1:50879] AH00681: Syntax error in type map, no ':' in…
jcubic
  • 230
  • 1
  • 4
  • 15
0
votes
0 answers

Internaly hosted alternative to cloudbased nobackend solution like Firebase

In an environment where I can't use a cloud based service like Firebase, I'm looking for recommendation for an internally hosted nobackend solution. I could use node or meteor but really am just wanting to get functionality to Firebase, without…
Nikos
  • 103
  • 1
  • 5
0
votes
2 answers

Gzip and Javascript Minification

I have a Rails app using the Rails javascript helpers to concat all the javascript together into a single file. In addition my web server is serving this javascript file to the end users as gzip compressed. As modern browsers will request the…
davidsmalley
  • 457
  • 1
  • 6
  • 14
0
votes
1 answer

deploying meteor on my own domain (ubuntu)

I want to deploy my meteor app on my Ubuntu server, I used meteor.sh and everything went successfully, But i simply can't launch my app from the browser I'm putting my ip address and getting a 404 The requested URL / was not found on this server. I…
Dany Y
  • 115
  • 6
0
votes
1 answer

Rogue iframe injection in my javascript

I ran my website through Sucuri http://sitecheck.sucuri.net/results/www.fort-aspenwood.com They came up with these rogue iframes in my minified javascript
Greatestswordsman
  • 397
  • 1
  • 7
  • 19
0
votes
1 answer

nginx not starting with multiple nodejs apps

i have this config in sites-enabled/default upstream domainone.com.ar { server 127.0.0.1:9000; } server { listen 80; server_name www.domainone.com.ar; rewrite ^/(.*) http://domainone.com.ar/$1 permanent; } server { listen 80; listen…