Topic on Project:Support desk

Is it okay to use mw.loader.load outside of MediaWiki:Common.js?

7
79.179.87.161 (talkcontribs)

I wish to load a JavaScript file locally, this way:

<html>
	<head>
	</head>
	<body>
		<iframe id="myIframe" src="https://example.com/x/y.php" width="100%" frameBorder="0" id="prcf_iframe"></iframe>
		<script>
			mw.loader.load("/iframe-resizer/js/iframeResizer.contentWindow.min.js");
			iFrameResize({ log: true }, '#myIframe')
		</script>
	</body>
</html>

Is it okay to use mw.loader.load outside of MediaWiki:Common.js?

Malyacko (talkcontribs)

What does "okay" mean? What would be "not okay"? What have you tried and what happened?

79.179.87.161 (talkcontribs)

"Okay" here means, is it possible to work with mw.loader.load (javascript file loader for MediaWiki) outside MediaWiki:Common.JS?

I've tried it and I got:

Uncaught ReferenceError: mw is not defined:
mw.loader.load("/iframe-resizer/js/iframeResizer.contentWindow.min.js");

How then should I load the JS file in the code above?

Bawolff (talkcontribs)

That would be a no (more about time rather than place). You can't use mw before its defined.

79.179.87.161 (talkcontribs)

@Bawolff Before it's defined? I am sorry, I didn't understand.

Why can't mw.loader.load be used outside MediaWiki:Common.js?

Bawolff (talkcontribs)

You can't use mw before the thing that creates it is run. MediaWiki:Common.js happens after mw is created, presumably whatever you are doing happens before.

TheDJ (talkcontribs)

Wrap the JS code in a function that only triggers after the document is ready, that's generally the safe way to do this.

Reply to "Is it okay to use mw.loader.load outside of MediaWiki:Common.js?"