Hello community,
in
http://www.cmsimpleforum.com/viewtopic. ... &sk=t&sd=a I've stumbled over problems with multiple jQuery versions.
Gert wrote:
I have tested the final first version of jQuery4CMSimple from Holger right now, we will release it now
Good news! That may solve the most problems, when all plugins are rewritten to use jQuery4CMSimple. But until than?
IMHO a good solution would be, when plugin authors would use
Code: Select all
var $myjquery = jQuery.noConflict();
as is described in Detail on
http://docs.jquery.com/Using_jQuery_wit ... _Libraries, und update their code accordingly.
This will even avoid problems with other Javascript libraries using $ (e.g. prototype.js). But so all the plugins must be rewritten -- and until than?
Another possible solution would be, to use only the newest jQuery version available. This is based on the assumption, that jQuery versions are upward compatible. The disadvantage: without using noConflict() there might still be problems with other js libraries. The idea was born in the above mentioned thread:
jerry wrote:
It's the way I read jQuery
Code: Select all
if (!preg_match('/<.*jquery([.|-]{0,1}\d*){0,3}([.]{0,1}[min|all|pack]*){0,2}[.]js.*>/i', $hjs)) {
$hjs.='<!-- include jQuery library -->
<script type="text/javascript" src="'.$pth['folder']['base'].$cf['plugins']['folder'].'/'.$plugin.'/js/jquery.min.js"></script>
<script type="text/javascript"> jQuery.noConflict();</script>';
}
But this way the plugin will not work, if there's already an older jQuery version included, that doesn't fit the plugins needs.
I have written the following function (to not pollute the global namespace any further; it's name should be adjusted to the particular plugin it's used in) to do the trick. It's heavily commented and self explaining (at least I hope so
) It should be adapted and called instead of manipulating $hjs "manually" to include jQuery.
Code: Select all
function pluginname_use_newest_jquery_version() {
global $hjs, $plugin, $pth;
// the pattern I'm looking for, to see if jquery is already in $hjs
$pattern = '/<script.*jquery-([0-9])\.([0-9])\.?([0-9])?.*?<\/script>/';
// the <script> to include if there is no jquery already included,
// or if my version is newer than the already included one
$my_jquery_include = '<script type="text/javascript" src="'.$pth['folder']['plugins']
.$plugin.'/jstree/_lib/jquery-1.5.1.js"></script>';
// the version of my jquery version
// (this should match the filename given above)
$my_jquery_version = array(1, 5, 1);
$has_jquery = preg_match($pattern, $hjs, $matches);
if (!$has_jquery) {
// then include new version
$hjs .= $jquery.PHP_EOL;
}
else {
// find out the version of already included jquery
$other_jquery_version = array_slice($matches, 1);
// compare version to include only newest
for ($i = 0; $i < 3; $i++) {
if ($my_jquery_version[$i] < $other_jquery_version[$i]) {
$my_jquery_version_is_newer = FALSE;
break;
} elseif ($my_jquery_version[$i] > $other_jquery_version[$i]) {
$my_jquery_version_is_newer = TRUE;
break;
} else {
// spare the preg_replace in case of same version:
$my_jquery_version_is_newer = FALSE;
// and continue comparison
}
}
if ($my_jquery_version_is_newer) {
// replace old jquery include with my one
$hjs = preg_replace($pattern, $my_jquery_include, $hjs);
}
}
}
The code is free for anyone to use and modify (let's say MIT license). Maybe this function could be useful for plugin authors until other plugins are updated to use jQuery4CMSimple, and they can't rewrite their code to use noConflict(). Maybe it could be used in jQuery4CMSimple itself.
Any comments appreciated.
Christoph
BTW: How can I use syntax highlighting?