« Mediawiki et JavaScript » : différence entre les versions
Apparence
m Nicolas a déplacé la page MediaWiki et JavaScript vers Mediawiki et JavaScript |
|||
Ligne 62 : | Ligne 62 : | ||
<kode lang=javascript> | <kode lang=javascript> | ||
jQuery(document).ready(function($) { | jQuery(document).ready(function($) { | ||
$("#searchInput").focus(); | |||
// focus on the search field while viewing only and when the page is on scrolled to top | |||
if (mw.config.get('wgAction') === 'view') | |||
{ | |||
if (window.scrollY === 0) { | |||
let searchForm = document.getElementById("searchform"); | |||
if (searchForm) | |||
searchForm.getElementsByTagName("input")[0].focus(); | |||
} | |||
} | |||
} | } | ||
</kode> | </kode> |
Version du 28 janvier 2025 à 19:47
Ajouter du JavaScript dans toutes les pages
Ajouter le code JavaScript à la page MediaWiki:Common.js
Documentation
The main JavaScript library
jQuery & plugins
List of JavaScript pages
Configuration variables
Variables prédéfinies
Nom | Valeur |
---|---|
wgServer | http://site.serveur.fr |
wgScript | /index.php |
// http://site.serveur.fr/fichier.ext url = wgServer + "/fichier.ext" |
mw.config
JavaScript predefined variables
Avec PHP, préfixer les noms des variables avec $ : $wgServer
Chargement d'un fichier Javascript
Common.js |
// Mediawiki.loader mw.loader.load( 'http://url/fichier.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400' ); // balise script document.write(unescape("%3Cscript src='" + wgServer + "/fichier.js' type='text/javascript'%3E%3C/script%3E")); // getScript $.getScript("/fichier.js", function() { ... }); |
addOnloadHook
Execute du code une fois la page chargée.
addOnloadHook(someFunction); addOnloadHook(function(){ ... }); |
Test du groupe auquel l'utilisateur appartient
// Si l'utilisateur n'appartient pas au groupe user if ($.inArray("user", wgUserGroups) == -1) { } |
Donner le focus au champs de recherche
jQuery(document).ready(function($) { $("#searchInput").focus(); // focus on the search field while viewing only and when the page is on scrolled to top if (mw.config.get('wgAction') === 'view') { if (window.scrollY === 0) { let searchForm = document.getElementById("searchform"); if (searchForm) searchForm.getElementsByTagName("input")[0].focus(); } } } |
// Adds Contact dans navigation ModifySidebar("add", "navigation", "Contact", "http://bananeatomic.kegtux.org/index.php/Spécial:Contact"); function ModifySidebar(action, section, name, link) { try { switch (section) { case "languages": var target = "p-lang"; break; case "toolbox": var target = "p-tb"; break; case "navigation": var target = "p-navigation"; break; default: var target = "p-" + section; break; } if (action == "add") { var node = document.getElementById(target) .getElementsByTagName('div')[0] .getElementsByTagName('ul')[0]; var aNode = document.createElement('a'); var liNode = document.createElement('li'); aNode.appendChild(document.createTextNode(name)); aNode.setAttribute('href', link); liNode.appendChild(aNode); liNode.className='plainlinks'; node.appendChild(liNode); } if (action == "remove") { var list = document.getElementById(target) .getElementsByTagName('div')[0] .getElementsByTagName('ul')[0]; var listelements = list.getElementsByTagName('li'); for (var i = 0; i < listelements.length; i++) { if (listelements[i].getElementsByTagName('a')[0].innerHTML == name || listelements[i].getElementsByTagName('a')[0].href == link) { list.removeChild(listelements[i]); } } } } catch(e) { // lets just ignore what's happened return; } } |