Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Javascript FAQ: Difference between revisions

From MoodleDocs
No edit summary
(17 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== What is JavaScript? ==
== What is JavaScript? ==


JavaScript is a scripting language widely used for client-side web development. It is also known by a number of other names including ECMAScript, and JScript (these are all the same thing but exist due to issues over the licensing of these names).
JavaScript is a scripting language widely used for client-side web development. It is also known as ECMAScript and has been progressively improved over the years.


If you have a spare 27 minutes and would like a quick background on JavaScript and it's origins, see this video from Douglas Crockford '[http://www.youtube.com/watch?v=gz7KL7ZirZc JSeverywhere: Douglas Crockford - JavaScript, The World's Most Misunderstood Language]'.
Since Moodle 3.8 we recommend the use of Native JavaScript rather than through a library such as jQuery or YUI, and require that all new JavaScript be written in the style of ES 2015 modules. These should be placed in the amd/src directory of a component and are transpiled at development time to be compatable with a wide range of browsers.


== Where do I find general information about JavaScript? ==
== Where do I find general information about JavaScript? ==
Line 9: Line 9:
=== Online resources: ===
=== Online resources: ===
* [http://en.wikipedia.org/wiki/JavaScript JavaScript (Wikipedia)]
* [http://en.wikipedia.org/wiki/JavaScript JavaScript (Wikipedia)]
* [http://articles.sitepoint.com/category/javascript Sitepoint JavaScript tutorials]
* [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide Mozilla JavaScript guide]


=== Books: ===
=== Books: ===
Line 18: Line 18:
== How is JavaScript used by Moodle? ==
== How is JavaScript used by Moodle? ==


* See [https://docs.moodle.org/en/Category:Javascript Category:Javascript].
* See [[Javascript]].


== Where do I find more information about JavaScript in Moodle? ==
== Where do I find more information about JavaScript in Moodle? ==
Line 26: Line 26:


== What JavaScript library does Moodle use? ==
== What JavaScript library does Moodle use? ==
=== JQuery ===
{{Deprecated|version=3.8}}
As of Moodle 3.8, the user of [[jQuery]] is generally discouraged and the use of Native or Vanilla Javascript is recommended.


=== Yahoo! User Interface Library (YUI) ===  
=== Yahoo! User Interface Library (YUI) ===
Moodle uses the Yahoo! User Interface Library (YUI).
{{Deprecated|version=2.9}}
As of Moodle 2.9, the user of YUI is generally discouraged.


==== Online resources: ====
==== Online resources: ====
* [http://yuilibrary.com Yahoo! User Interface Library (YUI)]
* [http://yuilibrary.com Yahoo! User Interface Library (YUI)]
* [[YUI]] in the Moodle documentation
* [[YUI]] in the Moodle documentation
* [https://alloyui.com/rosetta-stone/ Migrating YUI code to jQuery]


==== Books: ====
==== Books: ====
* [http://shop.oreilly.com/product/0636920013303.do YUI 3 Cookbook] by Evan Goer
* [http://shop.oreilly.com/product/0636920013303.do YUI 3 Cookbook] by Evan Goer
* [http://shop.oreilly.com/product/0636920025245.do Maintainable JavaScript] by Nicholas C. Zakas
* [http://shop.oreilly.com/product/0636920025245.do Maintainable JavaScript] by Nicholas C. Zakas
 
==== Why are we moving away from YUI ? ====
The decision to begin the transition from YUI to JQuery was made here:


==== How/Why was YUI chosen for Moodle? ====
https://tracker.moodle.org/browse/MDL-47036


The decision was made in [http://moodle.org/mod/forum/discuss.php?d=48478 this thread in the General Developer Forum].
This is in response to Yahoos announcement that they will be ceasing all further development on YUI (although it is still maintained).  
 
http://yahooeng.tumblr.com/post/96098168666/important-announcement-regarding-yui
Since this decision was made, it's continued use has been questioned several times. Some of the main reasons for it's use include:
 
* good documentation;
* an active developer community;
* code modularity;
* a powerful loader system separating loading from execution and adding dependency resolution; and
* sandboxing of code as standard.
 
Most other major libraries do not offer many of these major features including the powerful loader and dependency system which are particularly useful in the modular environment of Moodle.
 
== What other JavaScript libraries are around? ==
 
=== jQuery ===
 
jQuery is another popular JavaScript library, used among others by Drupal, Joomla and WordPress. It is probably the most widely known JavaScript library at present.
It is possible to use jQuery in Moodle, but it is not recommended.
 
'''JavaScript written using jQuery will not be accepted within core Moodle code.''' However since Moodle 2.5 jQuery and jQuery UI libraries are included in default installation, these are intended to be used in add-ons only. See [[jQuery]] page for more information.
 
==== Can't we switch to jQuery in core?====
Although jQuery is very popular with many developers and has stood out in recent years, there are a number of key infrastructure reasons which have the potential to cause major issues in a system like Moodle. As an example, some of these core infrastructure-type reasons include:
 
* jQuery does not encourage sandboxing of code
* jQuery does not have a decent modular system - plugins typically exist in the global $ namespace (and due to lack of sandboxing), this means that:
** they can be modified after loading so other modules on the same page could modify a library function; and as a result
** it is possible for jQuery to overwrite itself and break your page
* jQuery does not have a loading system. You are typically expected to use the <script> tag which:
** can be inefficient
** means that your code is executed as soon as it loads;
*** which means that load order is often very significant
** has mixed browser support with some of it's advanced features;
** makes it difficult to make use of a combo loading system to load JS modules.
 
 
Whilst these reasons may not seem very important as they're just nuts and bolts JS issues, when considered in combination with the modularity of Moodle itself they make a lot more sense. Bearing in mind that Moodle encourages use of contributed plugins (blocks, modules, question types, etc):
 
* a number of Moodle-specific YUI modules exist to encourage re-use of user interface components and functionality:
** so we need dependency management;
** and to ensure that load order of components does not matter;
** and that multiple uses of the same JS code does not break former or subsequent uses by it's mere inclusion.
* with the availability of contributed plugins for moodle we need to ensure that modules do not interfere with, or have negative effects on, one another;
* with the potential number of non-core plugins, sandboxing of that code is important to prevent inadvertent issues caused by individual plugins (e.g. changing core library code to add ''features'').
 
==== Online resources: ====
* [http://jquery.com/ jQuery]
* [http://visualjquery.com Visual jQuery]
* [http://jqueryfordesigners.com jQuery for Designers]
 
==== Books: ====
* [http://www.manning.com/bibeault/ jQuery in Action] by Bear Bibeault and Yehuda Katz
* [http://www.packtpub.com/jQuery/book Learning jQuery] by Karl Swedberg and Jonathan Chaffer
* [http://www.packtpub.com/jquery-reference-guide-Open-Source/book jQuery Reference Guide] by Jonathan Chaffer and Karl Swedberg
 
== What does AJAX mean? ==
AJAX means "Asynchronous JavaScript and XML". It is a group of interrelated web development techniques used to create interactive web applications or rich Internet applications.
Although XML is included in the acronym, AJAX is not a single technology but a group of technologies. In reality with modern JS, this tends to be JSON which is much more lightweight and easy to produce in PHP than XML.
 
See [http://en.wikipedia.org/wiki/Ajax_(programming) AJAX (Wikipedia)] for general information and [[AJAX]] for information on AJAX and Moodle.
 
See also [[Firebug#Debugging AJAX_with_Firebug|Firebug > Debugging AJAX with Firebug]].


== See also ==
== See also ==


* [[jQuery]]
* [[jQuery]]
* [[Javascript and YUI 3 FAQ]]


[[Category:FAQ]]
[[Category:FAQ]]
[[Category:Javascript]]
[[Category:Javascript]]
[[Category:AJAX]]
[[Category:AJAX]]

Revision as of 03:10, 14 April 2021

What is JavaScript?

JavaScript is a scripting language widely used for client-side web development. It is also known as ECMAScript and has been progressively improved over the years.

Since Moodle 3.8 we recommend the use of Native JavaScript rather than through a library such as jQuery or YUI, and require that all new JavaScript be written in the style of ES 2015 modules. These should be placed in the amd/src directory of a component and are transpiled at development time to be compatable with a wide range of browsers.

Where do I find general information about JavaScript?

Online resources:

Books:

How is JavaScript used by Moodle?

Where do I find more information about JavaScript in Moodle?

What JavaScript library does Moodle use?

JQuery

This feature has been marked as deprecated since Moodle 3.8

As of Moodle 3.8, the user of jQuery is generally discouraged and the use of Native or Vanilla Javascript is recommended.

Yahoo! User Interface Library (YUI)

This feature has been marked as deprecated since Moodle 2.9

As of Moodle 2.9, the user of YUI is generally discouraged.

Online resources:

Books:

Why are we moving away from YUI ?

The decision to begin the transition from YUI to JQuery was made here:

https://tracker.moodle.org/browse/MDL-47036

This is in response to Yahoos announcement that they will be ceasing all further development on YUI (although it is still maintained). http://yahooeng.tumblr.com/post/96098168666/important-announcement-regarding-yui

See also