#REDIRECT [[Wikipedia:User scripts/Guide]]
<!--Scripts with [[Ajax (programming)|Ajax]] usually do something without leaving the page.-->
==XMLHttpRequest==
Usual approach for creating cross-browser [[XMLHttpRequest]] object is<br />
<code>if (window.XMLHttpRequest) ... else if (window.ActiveXObject) ...</code>.
However, built-in [{{SERVER}}/skins-1.5/common/ajax.js ajax.js] already has this code, so userscripts can simply use
<source lang=javascript>
xmlhttp = sajax_init_object()
</source>
==Data sources==
===[{{SERVER}}{{SCRIPTPATH}}/api.php api.php]===
See [[mw:API]]. Usually used with [[JSON]] format.
===[{{SERVER}}{{SCRIPTPATH}}/query.php query.php]===
See [[User:Yurik/Query_API]]. This is older verison of API, can be used for some queries not yet implemented in API. Some Javascript examples can be found at [[User:Yurik/Query_API/User_Manual#JavaScript]].
===HTML===
You could fetch the whole article page or use <code>&action=render</code> URL parameter to get the content without all the menus ([{{fullurl:{{FULLPAGENAME}}|action=render}} example]). Also see [[mw:Manual:Parameters to index.php|Parameters to index.php]].
The result you could treat as a text but it's usually convenient to parse it as HTML document, e.g. using DOMParser object <small>(examples needed)</small>.
===Wiki code===
To get the wiki code you use <code>&action=raw</code> URL parameter ([{{fullurl:{{FULLPAGENAME}}|action=raw}} example]).
The result you treat as a text.
===Preview===
Sometimes you might want to use preview. For example, [[Special:Prefixindex]] won't work with <code>&action=render</code>. To get rid of the unnecessary menus you could submit <tt><nowiki>{{Special:Prefixindex/somepage}}</nowiki></tt> for a preview and get a "clean" list (nevertheless, it's better to use API for prefix index)
==Implementations==
===Mediawiki===
* [{{SERVER}}/skins-1.5/common/ajax.js ajax.js] - some support functions
* [{{SERVER}}/skins-1.5/common/ajaxwatch.js ajaxwatch.js] - watch/unwatch
* [{{SERVER}}/skins-1.5/common/upload.js upload.js] - licenses preview on [[Special:Upload]]
[http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/AjaxFunctions.php?view=markup AjaxFunctions.php] has 2 main functions:
* wfAjaxWatch - server part of ajaxwatch.js
* wfSajaxSearch - disabled on WMF projects. If it was enabled, [{{SERVER}}/skins-1.5/common/ajaxsearch.js ajaxsearch.js] would be responsible for the client side. Also see [[mw:Manual:$wgAjaxSearch]].
Another disabled feature is [[mw:Manual:Live preview|Live preview]] ([{{SERVER}}/skins-1.5/common/preview.js preview.js])
===Userscripts===
* Simple library for Ajax editing: [[User:TheFearow/simpleajax.js|simpleajax]]
* Ajax preview: [[User talk:Alex Smotrov/qpreview.js|qPreview]]
* Ajax search: [[User:Zocky/Auto_Complete|Auto Complete]] (Firefox only), uses query.php
* Ajax unwatch from watchlist: [[User talk:Alex Smotrov/wlunwatch.js|wlUnwatch]]
* [[Wikipedia:Tools/Navigation popups|Tools/Navigation popups]]
* [[Wikipedia:WikiProject User scripts/Scripts/Twinkle|Twinkle]] (some code in [[User:AzaToth/morebits.js]], and good examples of AJAX are in the bottom of [[User:AzaToth/twinklespeedy.js]])
==Misc==
Some alternatives to XMLHttpRequest:
* [[IFrame]]: almost nobody uses this, but if you're interested, here's a small library: [[User:TheFearow/ajax.js]] (Warning: Untested)
* URL actions: this is not Ajax at all, but this method is also widely used for one-click-do-all scripts. 1st part of the script goes to another page using additional "dummy" URL parameters (not recognized and so ignored by Mediawiki). 2nd part of the script analyzes that URL and then does something. Because of the way MediaWiki redirects after edits, cookies may also be used to transfer data and instructions. An example library (unmaintaned) can be found at [[User:Lupin/autoedit.js]]
|