Intergating dokuwiki and jquery
Dokuwiki us prototype, my site use Jquery.
If there is another way to alter Dokuwiki javascript, instead using jQuery.noConflict(), or/and var $j = jQuery on my jQuery?
I open all .js files on Dokuwiki, s开发者_JAVA百科earch for "$(" and replace it with "$DW(", and it works like a charms, but I had to deal with every Dokuwiki plugins that has javascript in it.
So, how to alter "$" in prototype libaries/ Dokuwiki?
You are doing it the wrong way around. Just include jQuery after prototype then immediately make a call to jQuery.noConflict()
. And in your code always write
jQuery(..)
/ jQuery.[functionname](...)
instead of the usual $
notation.
This way you can leave the whole existing javascript source of dokuwiki and all existing plugins untouched. And you can use jQuery in your code.
Or if you still want a short handle instead of writing jQuery you could create an alias for jQuery
<script ... include prototype ..</script>
<script ... include jquery ..</script>
<script ...>
var jQ = jQuery.noConflict();
//do something with prototype
$("findme");
jQ("div hideme").hide();
</script>
For more info check jQuery.noConflict()
The way I did that was to create a very simple "plugin" that does nothing else than include an extra JS file in each page's header section.
All you need to do is to add a new directory under lib/plugins
which contains a "plugin.info.txt" (see the other plugins for examples) and one file called "action.php" (plus any files you want to load, in this case a .js file).
That "action.js" file should just contain a class that extends "DokuWiki_Action_Plugin
" with two functions:
Firstly, "register" to, well, register an event handler for when metadata is output:
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this,
'handle_tpl_metaheader_output');
}
Secondly, the actual event handler, which is simply:
public function handle_tpl_metaheader_output(Doku_Event $event, $param) {
global $ID, $conf, $INFO;
if (page_exists($ID)) {
$event->data["script"][] = array (
"type" => "text/javascript",
"src" => DOKU_BASE."lib/plugins/[YOURPLUGINNAME]/[YOURJSFILENAME].js"
);
}
}
And that's it. Then do as you please in the JS file.
Edit: of course, you still need to refer to jQuery by using "jQuery()
" instead of "$()
".
精彩评论