devel:javascript
Различия
Показаны различия между двумя версиями страницы.
devel:javascript [2023/08/28 13:52] – создано vladpolskiy | devel:javascript [Дата неизвестна] (текущий) – удалено - внешнее изменение (Дата неизвестна) 127.0.0.1 | ||
---|---|---|---|
Строка 1: | Строка 1: | ||
- | ====== JavaScript ====== | ||
- | DokuWiki makes uses of [[wp> | ||
- | |||
- | This page gives you an overview how JavaScript is loaded from DokuWiki core, [[: | ||
- | |||
- | ===== JavaScript Loading ===== | ||
- | |||
- | All JavaScript code is collected from all found files and concatenated as one block of code. It is then whitespace compressed (if [[config: | ||
- | |||
- | DokuWiki will load JavaScript from the following places: | ||
- | |||
- | * autogenerated JavaScript (language strings, config settings, [[: | ||
- | * lib/ | ||
- | * lib/ | ||
- | * lib/ | ||
- | * conf/ | ||
- | |||
- | As you can see you can provide JavaScript with your [[: | ||
- | |||
- | ==== Deferred Loading ==== | ||
- | |||
- | All JavaScript is loaded with the [[https:// | ||
- | |||
- | If you load JavaScript through other means than the recommended methods below and that JavaScript has dependencies on any DokuWiki provided code, you need to ensure it is deferred as well. This also means that you cannot use '' | ||
- | |||
- | From the 2020 Hogfather version onwards, all javascript must be load in a deferred way. To update your existing templates javascript to work in this version, you can add the '' | ||
- | |||
- | Temporary, the feature flag [[config: | ||
- | |||
- | ==== Include Syntax ==== | ||
- | |||
- | DokuWiki' | ||
- | |||
- | :!: Included files are not checked for updates by the cache logic. You need to touch the master file for updating the cache. | ||
- | |||
- | :!: Includes are **not** supported inside included files to avoid any circular references. | ||
- | |||
- | :!: Includepath may only consist of letter, | ||
- | |||
- | === include === | ||
- | |||
- | < | ||
- | /* DOKUWIKI: | ||
- | </ | ||
- | |||
- | This syntax will include the given file where the comment is placed. The filename is relative to the file containing the include markup unless it starts with a slash which indicates an absolute URL path. | ||
- | |||
- | === include_once === | ||
- | |||
- | < | ||
- | /* DOKUWIKI: | ||
- | </ | ||
- | |||
- | This syntax will include the given file where the comment is placed. The filename is relative to the file containing the include markup unless it starts with a slash which indicates an absolute URL path. | ||
- | |||
- | The file will only be included if not a file of the same base name was previously loaded through the include_once statement. This name is shared over all script files (from all plugins), so you should use a meaningful file name. | ||
- | |||
- | Using this statement makes sense if you write multiple independent [[: | ||
- | |||
- | ===== Coding Guidelines ===== | ||
- | |||
- | When writing JavaScript for the use within DokuWiki you should follow a few rules. Because of the nature of JavaScript, failing to do so might result in not only breaking your script but all scripts in DokuWiki. | ||
- | |||
- | ==== Validate your Code ==== | ||
- | |||
- | As mentioned above, DokuWiki will shrink the JavaScript code when the [[config: | ||
- | |||
- | To check your code you should use the [[http:// | ||
- | |||
- | * debug your code with [[config: | ||
- | * verify your code still works with [[config: | ||
- | |||
- | ==== Use unobtrusive JavaScript ===== | ||
- | |||
- | Do not assume people have JavaScript enabled, when writing new DokuWiki functionality. Instead use JavaScript as enhancement of the user interface only, when JavaScript is not available your code should fallback to normal page reload based behavior. | ||
- | |||
- | jQuery makes this very easy. | ||
- | |||
- | ==== Avoid Inappropriate Mixing ===== | ||
- | |||
- | The old way of doing things is to embed JavaScript directly in the HTML. However, JavaScript and (X)HTML shouldn' | ||
- | |||
- | <code html4strict> | ||
- | <body onload=" | ||
- | |||
- | < | ||
- | |||
- | <script language=" | ||
- | doSomethingHere(); | ||
- | </ | ||
- | |||
- | < | ||
- | |||
- | </ | ||
- | </ | ||
- | |||
- | This isn't just a matter of philosophical purity: some of the JavaScript may not work. In the above example, it turns out that both DokuWiki and the '' | ||
- | |||
- | Strictly speaking, it is possible to embed JavaScript in your HTML, but only if you know that the JavaScript has no conflict with DokuWiki. Because this requires knowledge of DokuWiki' | ||
- | |||
- | ==== Using IDs ==== | ||
- | |||
- | To modify a DOM object the JavaScript must be able to locate the object. The easiest way to locate the object is to give the associated HTML tag an ID. This ID must be unique among all IDs on the page so that referencing this ID produces exactly the right DOM object. | ||
- | |||
- | When you are producing your own HTML (e.g. from a template or plugin) that should be accessed from JavaScript later, be sure that the ID does not conflict with an existing ID. In particular, be sure that it won't conflict with the IDs automatically assigned to section headers. The easiest way to ensure this is to use two adjacent underscores ('' | ||
- | |||
- | ==== Inline scripts ==== | ||
- | |||
- | As said before you should avoid mixing JavaScript and XHTML. However if you need to use inline JavaScript, you should wrap it like this: | ||
- | |||
- | < | ||
- | <script type=" | ||
- | ... | ||
- | / | ||
- | </ | ||
- | |||
- | If you need to add inline JavaScript to the < | ||
- | ===== jQuery ===== | ||
- | |||
- | Since the October 2011 release " | ||
- | |||
- | Please follow these coding conventions when working with jQuery in DokuWiki. Please also refer to our [[devel: | ||
- | |||
- | ==== No $() ==== | ||
- | |||
- | jQuery is only used in compatibility mode. There is no '' | ||
- | |||
- | Do not map '' | ||
- | |||
- | ==== Prefix jQuery object variables ==== | ||
- | |||
- | To make it clear that a variable contains an instance of the jQuery object all these variables should be prefixed by a '' | ||
- | |||
- | <code javascript> | ||
- | var $obj = jQuery('# | ||
- | </ | ||
- | |||
- | |||
- | ===== DokuWiki JavaScript Environment ===== | ||
- | |||
- | ==== Predefined Global Variables ==== | ||
- | |||
- | DokuWiki defines certain JavaScript variables for the use in your script: | ||
- | |||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | ==== JSINFO ==== | ||
- | |||
- | DokuWiki passes the global [[devel: | ||
- | * '' | ||
- | * '' | ||
- | And after 2018-04-05: | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | |||
- | Other keys can easily be added from within PHP code. The usual way is using an [[action plugin]] like this: | ||
- | |||
- | <code php> | ||
- | function register(Doku_Event_Handler $controller) { | ||
- | $controller-> | ||
- | } | ||
- | function _adduser(& | ||
- | global $JSINFO; | ||
- | $JSINFO[' | ||
- | } | ||
- | </ | ||
- | |||
- | If you want to make sure that your plugin' | ||
- | |||
- | The contents of the '' | ||
- | |||
- | If you need JSINFO in the [[: |
devel/javascript.1693219962.txt.gz · Последнее изменение: — vladpolskiy