2

Hi my problem is that i cant load some javascript file @ my special page extension. I tried it with addscript and some other methods but the only thing what happened was that the javascript was canceled cause no-js of the mediawiki software.

In the folder of my extension is a new.js file which i want to have access only on my special page.

Here is some code (most of it of the example of the special pages).

MyExentions.php

<?php
if (!defined('MEDIAWIKI')) {
    echo <<<EOT
To install my extension, put the following line in LocalSettings.php:
require_once( "$IP/extensions/MyExtension/MyExtension.php" );
EOT;
    exit( 1 );
}

$wgExtensionCredits['specialpage'][] = array(
    'path' => __FILE__,
    'name' => '-',
    'author' => 'Thomas Döring',
    'descriptionmsg' => '-',
    'version' => '0.0.1',
);

$dir = dirname(__FILE__) . '/';
$wgAutoloadClasses['SpecialMyExtension'] = $dir . 'SpecialMyExtension.php'; 
$wgExtensionMessagesFiles['MyExtension'] = $dir . 'MyExtension.i18n.php';
$wgExtensionMessagesFiles['MyExtensionAlias'] = $dir . 'MyExtension.alias.php'; 
$wgSpecialPages['MyExtension'] = 'SpecialMyExtension'; 

SpecialMyExtension.php

 <?php
 class SpecialMyExtension extends SpecialPage {
    function __construct() {
            parent::__construct( 'MyExtension' );
    }

    function execute( $par ) {
            $request = $this->getRequest();
            $output = $this->getOutput();
            $this->setHeaders();

            # Get request data from, e.g.
            $param = $request->getText('param');

            # Do stuff
            # ...

            if(file_exists("extensions/TimeLine/TimeLine/data.xml"))
            {
                    $data = simplexml_load_file("extensions/TimeLine/TimeLine/data.xml");

                    foreach($data->event as $event)
                    {
                        $html.="<tr><td>".$event['title']."</td><td>".$event['start']."</td></tr>";
                    }
                    $html.="</table>";

                    $html.="<a href=\"javascript:hello()\">klick</a>";

                    $output->addHTML($html);

                 }
                 else
                 {
                    $wikitext = 'Datei nicht gefunden!';
                    $output->addWikiText( $wikitext );
                 }

    }


 }
 ?>

I hope you can help me.

2
  • Where does your code contain any approaches to add the JS? Commented Oct 22, 2012 at 20:22
  • You know the RessourceLoader? Commented Oct 22, 2012 at 20:24

2 Answers 2

2

addScript works in version 1.16 and before. In 1.17 and later you should use addHeadItem:

$wgHooks['ParserBeforeTidy'][] = 'wgAddJquery';

function wgAddJquery(&$parser, &$text) {

  global $addJqueryScripts;
  if ($addJqueryScripts === true) return true;

  $parser->mOutput->addHeadItem(
    '<script language="JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>'
  );

  $addJqueryScripts = true;

  return true;

}
Sign up to request clarification or add additional context in comments.

2 Comments

Where do you put this AddHeadItem ? In the skin ?
Just an addendum: According to newer plugin conventions, you can put the hook registration to MyExtention.php, like this: $wgHooks['ParserBeforeTidy'][] = 'MyExtention::wgAddJquery'; and the function wgAddJQuery() as static method in MyExtention.hooks.php. This works for me and I have not altered LocalSettings.php nor am I bound to a specific skin.
0

I added it in the skin file, in the function function setupSkinUserCss $out->addHeadItem('maketree.js',"

<script type='text/javascript' src='/js/mktree.js'></script>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.