Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 views
in Technique[技术] by (71.8m points)

php - Mediawiki Extension add Javascript in Header

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...