Jump to content

Topic on Project:Support desk/Flow

jQuery is not defined when upgrade from 1.20 to 1.25

4
Chan15tw (talkcontribs)

I have written my extension when I use 1.20, and there is some Javascript in the code, for example

<?php

class Example

{

public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin )

{

echo '<script>';

echo '$(function() { console.log("test") });'

echo '</script>';

}

It works perfectly in 1.20, but after I upgrade MediaWiki to 1.25, I got message jQuery is not defined(…), I am pretty sure jQuery is loaded, because I type $('body').length on console panel, it returns 1, how to fix it?

87.123.45.172 (talkcontribs)

I don't know, what you are actually trying to do. However, if you need jQuery, then the right way is not to output your JavaScript code into the page directly. Instead, include your code inside a ResourceLoader module.

137.147.144.56 (talkcontribs)

You can't just insert scripts that depend on other scripts like that, as everything is loaded asynchronously since 1.26. As said above, making a RL module is the best way. If there is some reason you really have to have an inline script, then in currently supported versions of MW (1.26+) you can add your script to the RL queue, so it will be executed after the core modules (mediawiki and jQuery) are ready.

(window.RLQ=window.RLQ||[]).push(function(){
    $(function() { console.log("test") });
});
Chan15tw (talkcontribs)

ResourceLoader solve my problem, I will also try RLQ, thanks all