Stop Loading Automatic Scripts in Joomla 2.5 - joomla

In Joomla 2.5, the scripts below are loaded automatically.
<script src="/media/system/js/mootools-core.js" type="text/javascript"></script>
<script src="/media/system/js/core.js" type="text/javascript"></script>
<script src="/media/system/js/caption.js" type="text/javascript"></script>
<script src="/media/system/js/mootools-more.js" type="text/javascript"></script>
<script src="/templates/pswedge/js/jquery.min.js" type="text/javascript" defer="defer"></script>
I don't want to load these files. How can I remove these links?

I would not recommend you to change the core files of Joomla.But if you really need to that then you can try this:
Step to disable the preloaded script file in joomla template.
Step one: Using your favorite file editor, open for edit:
/libraries/joomla/document/html/renderer/head.php
Step one: Find this code at line 151 and update it to include the code with this :
// Generate script file links
foreach ($document->_scripts as $strSrc => $strAttr)
{
// Code to disable mootools for your site (still loads it for your admin)
$ex_src = explode("/",$strSrc);
$js_file_name = $ex_src[count($ex_src)-1];
$js_to_ignore = array("mootools-core.js","mootools-more.js","core.js","caption.js");
if( in_array($js_file_name,$js_to_ignore) AND substr_count($document->baseurl,"/administrator") < 1 AND $_GET['view'] != 'form')
continue;
$buffer .= $tab . '<script src="' . $strSrc . '"';
if (!is_null($strAttr['mime']))
{
$buffer .= ' type="' . $strAttr['mime'] . '"';
}
if ($strAttr['defer'])
{
$buffer .= ' defer="defer"';
}
if ($strAttr['async'])
{
$buffer .= ' async="async"';
}
$buffer .= '</script>' . $lnEnd;
}
After saving the changes above, clear the cache that you have set and test your Joomla website and your Joomla Admin Dashboard. If you view the source code,all the predefind files are not there.
Or
You can try like this to hide it from index.php in template. Just put this line before the <jdoc:include type="head" /> and make necessary changes as needed to the scripts.
<?php
$search = array('mootools-more.js', 'caption.js');
// remove the js files
foreach($this->_scripts as $key => $script) {
foreach($search as $findme) {
if(stristr($key, $findme) !== false) {
unset($this->_scripts[$key]);
}
}
}
?>

May one of these method work-
Method 1:
Put Before <jdoc:include type="head" />
$search = array('mootools', 'caption.js');
// remove the js files
foreach($this->_scripts as $key => $script) {
foreach($search as $findme) {
if(stristr($key, $findme) !== false) {
unset($this->_scripts[$key]);
}
}
}
//Method 2
in index.php of template
$parameter_script = 'scripts';
$headerstuff=$document->getHeadData();
reset($headerstuff[$parameter_script]);
foreach($headerstuff[$parameter_script] as $key=>$value){
unset($headerstuff[$parameter_script][$key]);
}
$document->setHeadData($headerstuff);
//Method 3
In the file directory /plugins/system create a new file called “removemootools.php” and insert the following code (you will need to register the plugin in the database, too).
class plgSystemRemoveMooTools extends JPlugin
{
public function onAfterDispatch()
{
$app = JFactory::getApplication();
if($app->isSite()) //Only ever remove MooTools from the client-side, never the admin side
{
//Repeat these three line for all js you want to exclude
$mootools = JURI::root(true).DS.'media'.DS.'system'.DS.'js'.DS.'mootools.js';
$document = JFactory::getDocument();
unset($document->_scripts[$mootools]);
}
}
}

I used this code to remove jquery files from the head ( Joomla! 3.3.1 )
<?php
//Remove jquery
$search = array('jquery', 'jquery.min.js');
// remove the js files
foreach($this->_scripts as $key => $script) {
foreach($search as $findme) {
if(stristr($key, $findme) !== false) {
unset($this->_scripts[$key]);
}
}
}
?>
<jdoc:include type="head" />

Related

How do I stop Joomla from including jQuery?

I've recently upgraded from Joomla 3.2.1 to Joomla 3.2.2.
In Joomla 3.2.1, I manually unset jQuery from being included:
$doc = JFactory::getDocument();
$dontInclude = array(
'/media/jui/js/jquery.js',
'/media/jui/js/jquery.min.js',
'/media/jui/js/jquery-noconflict.js',
'/media/jui/js/jquery-migrate.js',
'/media/jui/js/jquery-migrate.min.js',
'/media/jui/js/bootstrap.js',
'/media/system/js/core-uncompressed.js',
'/media/system/js/tabs-state.js',
'/media/system/js/core.js',
'/media/system/js/mootools-core.js',
'/media/system/js/mootools-core-uncompressed.js',
);
foreach($doc->_scripts as $key => $script){
if(in_array($key, $dontInclude)){
unset($doc->_scripts[$key]);
}
}
But this isn't working in Joomla 3.2.2. Is there a way to not include Joomla's jQuery in 3.2.2?
Another variation which works well for me with Joomla 3.4 is to edit the template > index.php file with something like:
$doc = JFactory::getDocument();
$headData = $doc->getHeadData();
$scripts = $headData['scripts'];
//scripts to remove, customise as required
unset($scripts[JUri::root(true) . '/media/system/js/mootools-core.js']);
unset($scripts[JUri::root(true) . '/media/system/js/mootools-more.js']);
unset($scripts[JUri::root(true) . '/media/system/js/core.js']);
unset($scripts[JUri::root(true) . '/media/system/js/modal.js']);
unset($scripts[JUri::root(true) . '/media/system/js/caption.js']);
unset($scripts[JUri::root(true) . '/media/jui/js/jquery.min.js']);
unset($scripts[JUri::root(true) . '/media/jui/js/jquery-noconflict.js']);
unset($scripts[JUri::root(true) . '/media/jui/js/bootstrap.min.js']);
unset($scripts[JUri::root(true) . '/media/jui/js/jquery-migrate.min.js']);
$headData['scripts'] = $scripts;
$doc->setHeadData($headData);
You need to add a prefix of JUri::root(true) before each of those file names - relative paths will not work
I've added:
$doNotInclude = array(
'jquery',
'bootstrap',
'behavior',
);
if(in_array($file, $doNotInclude)){
return;
}
immediately after:
list($key, $prefix, $file, $func) = static::extract($key);
in libraries/cms/html/html.php, in the "_" function.
I don't like it since its a modification to the Joomla core but it works. I'm still looking for a better solution.
You can also try something like this:
$removeScripts = [
'/media/jui/js/jquery.min.js',
'/media/jui/js/jquery-noconflict.js',
'/media/jui/js/jquery-migrate.min.js',
'/media/system/js/caption.js',
];
foreach ($removeScripts as $removeScript) {
unset($this->_scripts[JURI::root(true).$removeScript]);
}
The problem is with your in_array.
If you remove it by changing this:
foreach($doc->_scripts as $key => $script){
if(in_array($key, $dontInclude)){
unset($doc->_scripts[$key]);
}
}
to this:
foreach($doc->_scripts as $key => $script){
unset($doc->_scripts[$key]);
}
Then it works fine. It's pretty pointless checking if the array key exists as I gathered you haven't manually deleted any of these files yourself.
Hope this helps
Joomla 3.3.6 loads scripts in different way so $doc->_scripts will return nothing... so there is nothing to unset.
I recommend to use this plugin: https://github.com/Poznakomlus/joomla_options
It allows you to remove bootstrap, jQuery and mootools (you can choose what to disable).
Disclaimer: I'm not affiliated any way with plugin developer or plugin itself in any way.
If you are writing a custom template or a component, where you need to remove all the scripts loaded by default inside Joomla you can create a simple plugin and bind the execution to the onBeforeCompileHead event.
My implementation was as below. Its very simple. You can further play around with the search list, by being specific to file names or just plain blacklisting the parent folder.
protected $app;
public function onBeforeCompileHead() {
// Front end
if ($this->app instanceof JApplicationSite) {
$doc = JFactory::getDocument();
$search = array(
'jui/js/',
'system/js/'
);
foreach ($doc->_scripts as $key => $script) {
foreach ($search as $findme) {
if (stristr($key, $findme) !== false) {
unset($doc->_scripts[$key]);
}
}
}
}
}
This worked for me in joomla 3.9
<?php
defined('_JEXEC') or die('Restricted access');
$unset_scripts = [
'/media/jui/js/jquery.min.js',
'/media/jui/js/jquery-noconflict.js',
'/media/jui/js/jquery-migrate.min.js',
'/media/system/js/caption.js',
];
foreach ($unset_scripts as $script) {
unset($this->_scripts[JURI::root(true) . $script]);
}
if (isset($this->_script['text/javascript'])) {
$captionJsStr = '%jQuery\(window\)\.on\(\'load\',\s*function\(\)\s*{\s*new\s*JCaption\(\'img.caption\'\);\s*}\);\s*%';
$this->_script['text/javascript'] = preg_replace($captionJsStr, '', $this->_script['text/javascript']);
if (empty($this->_script['text/javascript'])) {
unset($this->_script['text/javascript']);
}
}
$this->_scripts = array();
?>
<!DOCTYPE html>

Joomla 2.5: Distinct based on the language website with navigator

I have a joomla 2.5 bilingual website and I have the following code in index.php
<script type="text/javascript"> // <![CDATA[
if ( (navigator.userAgent.indexOf('Android') != -1) ) {
document.location = "a.html";
} // ]]>
</script>
As a result if you login from your Android phone/tablet it will lead you at the a.html link.
What I wanna do is:
Assume the website's url is the www.test.com/index.php?lang=en so when you login from android it leads you at the a.html and when you login from www.test.com/index.php?lang=fr should lead you at the b.html .
I need to distinct based on the language.
Thanks in advance.
You could maybe use something like this:
<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault( 'en-GB' )) { ?>
<script type="text/javascript"> // <![CDATA[
if ( (navigator.userAgent.indexOf('Android') != -1) ) {
document.location = "a.html";
} // ]]>
</script>
<?php }
elseif ($menu->getActive() == $menu->getDefault( 'fr-FR' )) { ?>
<script type="text/javascript"> // <![CDATA[
if ( (navigator.userAgent.indexOf('Android') != -1) ) {
document.location = "b.html";
} // ]]>
</script>
<?php }
?>
Please note I haven't tested this so let me know if it work and if not, I can update it.
Update:
As Oriol said, it's better to redirect server side rather than javascript. I was messing around with the code yesterday and half of what I wrote worked:
<?php
$lang = JFactory::getLanguage();
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault( 'en-GB' )) {
?>
<script type="text/javascript">
alert("<?php echo $lang->getTag() ?>");
</script>
<?php
}
elseif ($menu->getActive() == $menu->getDefault( 'fr-FR' )) {
?>
<script type="text/javascript">
alert("<?php echo $lang->getTag() ?>");
</script>
<?php
}
?>
It basically gets the current language tags and alerts then, depending which language the website is being viewed in. So it will either alert en-GB or fr-FR. Try using a server side redirect inside the if and else else statements

AJAX Div Refresh with PHP

I am trying to refresh some elements on my page every so often. I know theres a million topics on here about that and I have tried to get mine working, but here is what I need to refresh..
This is the code that gets generated when the page loads:
<div id="galleria">
<?php
$a = array();
$dir = '../public/wp-content/uploads/2012/01';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (preg_match("/\.png$/", $file)) $a[] = $file;
elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
}
closedir($handle);
}
$totalImgs = count($a);
$imgUsed = array();
for ($j = 0; $j < 100; $j++)
{
do
{
$randIndex = mt_rand(0, $totalImgs);
}
while ($imgUsed[$randIndex] === TRUE);
$imgUsed[$randIndex] = TRUE;
echo "<img src='" . $dir . '/' . $a[$randIndex] . "' />";
}
?>
</div>
I would like to automatically refresh this every 10 seconds but not reload the page. I have read up on ajax and it seems this is possible but I cannot seem to get it to work.
All this is doing is showing the galleria div, and loading the 100 images inside the div. Then the galleria script takes over and displays it nicely. Will AJAX work better or JQuery?
Thank you for your help!
"Will AJAX work better or jQuery?" -- AJAX is a technique, jQuery is a library. As it turns out, jQuery has an excellent API for AJAX.
Let's call this bit of PHP "galleria.php". On original page load, it is inserted into the parent PHP page using good ol' <?php include('galleria.php')?>. Now the end user is seeing the full initialized page.
To update it, you have a number of AJAX options available, but the easiest is to include jQuery on your page and then you can use .load() in a script:
var updateGallery = setInterval(function() {
$('#someDiv').load('galleria.php');
}, 10000);
There's room for tweaking... maybe galleria.php doesn't include the <div id="galleria">, which is set on the page. In which case you would load right into #galleria instead of #someDiv and save yourself an unnecessary container. Maybe you cache the $('#someDiv') object by declaring it in a different scope so that it can be re-used. But this is the general gist.
Use setInterval function with ajax call.
http://jquery-howto.blogspot.com/2009/04/ajax-update-content-every-x-seconds.html
As I wrote here you can fill a div with a jQuery ajax call.
<html>
<head>
<script type="text/javascript">
function refresh_gallery(){
$.ajax({
type: "POST",
url: "generate_gallery.php", // your PHP generating ONLY the inner DIV code
data: "showimages=100",
success: function(html){
$("#output").html(html);
}
});
}
$(function() {
refresh_gallery(); //first initialize
setTimeout(refresh_gallery(),10000); // refresh every 10 secs
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>

CodeIgniter and layouts? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What CodeIgniter library can provide this funcionality??
http://media.railscasts.com/videos/008_content_for.mov
It seems so simple on rails but I just cant find a simple way to achieve this on codeigniter.. please help.. I hate having to insert my styles or javascript in the body of the document
I hate templates like Smarty or http://williamsconcepts.com/ci/codeigniter/libraries/template/
Using my own implementation of Zend-like layout.
Add hook /application/hooks/Layout.php:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
*
*/
class Layout {
function render() {
global $OUT;
$CI = & get_instance();
$output = $CI->output->get_output();
if (!isset($CI->layout)) {
$CI->layout = "default";
}
if ($CI->layout != false) {
if (!preg_match('/(.+).php$/', $CI->layout)) {
$CI->layout .= '.php';
}
$requested = BASEPATH . '../application/layouts/' . $CI->layout;
$default = BASEPATH . '../application/layouts/default.php';
if (file_exists($requested)) {
$layout = $CI->load->file($requested, true);
} else {
$layout = $CI->load->file($default, true);
}
$view = str_replace("{content}", $output, $layout);
$view = str_replace("{title}", $CI->title, $view);
$scripts = "";
$styles = "";
$metas = "";
if (count($CI->meta) > 0) { // Массив с мета-тегами
$metas = implode("\n", $CI->meta);
}
if (count($CI->scripts) > 0) { // Массив со скриптами
foreach ($CI->scripts as $script) {
$scripts .= "<script type='text/javascript' src='" . base_url() . "js/" . $script . ".js'></script>";
}
}
if (count($CI->styles) > 0) { // Массив со стилями
foreach ($CI->styles as $style) {
$styles .= "<link rel='stylesheet' type='text/css' href='" . base_url() . "css/" . $style . ".css' />";
}
}
if (count($CI->parts) > 0) { // Массив с частями страницы
foreach ($CI->parts as $name => $part) {
$view = str_replace("{" . $name . "}", $part, $view);
}
}
$view = str_replace("{metas}", $metas, $view);
$view = str_replace("{scripts}", $scripts, $view);
$view = str_replace("{styles}", $styles, $view);
$view = preg_replace("/{.*?}/ims", "", $view); // Подчищаем пустые неподгруженные части шаблона
} else {
$view = $output;
}
$OUT->_display($view);
}
}
?>
Then enable hooks in your /application/config/config.php:
$config['enable_hooks'] = TRUE;
After that add to /application/config/hooks.php:
$hook['display_override'][] = array('class' => 'Layout',
'function' => 'render',
'filename' => 'Layout.php',
'filepath' => 'hooks'
);
Create folder /application/layouts/
Create file /application/layouts/default.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
{metas}
{scripts}
{styles}
<link rel="icon" href="<?= base_url() ?>favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="<?= base_url() ?>favicon.ico" type="image/x-icon" />
</head>
<body>
<div class="head">
{head}
</div>
<div class="content">
{content}
</div>
</body>
</html>
Use it from controller:
class main extends MY_Controller {
function __construct() {
parent::Controller();
$this->parts[head] = $this->load->view("frontend/global/header.php", null, true);
$this->scripts = array("JQuery/jquery-1.4.2.min", "JQuery/form", "Core", "Frontend");
$this->styles = array("style");
$this->title = "Blah blah";
}
function index() {
$this->load->view('frontend/index.php');
}
}
I've achieved this in the past using this Template Library. From the documentation:
The Template library, written for the
CodeIgniter PHP-framework, is a
wrapper for CI’s View implementation.
Template is a reaction to the numerous
questions from the CI community
regarding how one would display
multiple views for one controller, and
how to embed “views within views” in a
standardized fashion. In addition,
Template provides extra Views loading
capabilities, the ability to utilize
any template parser (like Smarty), and
shortcuts for including CSS,
JavaScript, and other common elements
in your final rendered HTML.
Specifically, check out the library's additional utilities. It allows you to put something like this in your controller:
$this->template->add_js('js/YourJavascriptFile.js');
or
$this->template->add_js('alert("Hello!");', 'embed');
Echo $_scripts in your template
(preferably in the <head> section) to
utilize JavaScript added via this
method.
Similarly, you can do the same with your CSS:
$this->template->add_css('css/main.css');
$this->template->add_css('#logo {display: block}', 'embed', 'print');
..and simply echo $_styles in your <head> section.
Hope that helps.
Check out Twig - it supports "inheritance" (instead of the traditional include header, include footer, etc)
http://www.twig-project.org/
CodeIgniter helper:
http://github.com/jamiepittock/codeigniter-twig
or
http://github.com/MaherSaif/codeigniter-twig
(haven't tried either, but looks like the latter is a fork w/the latest commits)
I had the same problem with finding a advance layout library as well,
so i wrote my own version which also fits your need (using YAML file).
Features
Manage layouts, page title, metas, css and js with YAML file for cleaner and lighter controllers.
Default configs can be overwritten in controller configs. So that you can split your css and js into smaller files for more flexible design.
Combine and minify css and js files in production mode for faster page loading.
Usage
// in the controller
$this->view->render();
This lib will get the layout, view, page title, css, js, metas from your YAML configs
Documentation
Please see this post for more detail
download
Download source code from Github

WordPress plugin: finding the <!--more--> in the_content

I'm writing a WordPress plugin that filters the_content, and I'd like to make use of the <!--more--> tag, but it appears that it has been stripped out by the time it reaches me. This appears to be not a filter, but a function of the way WordPress works.
I could of course resort to reloading the already-loaded content from the database, but that sounds like it might cause other troubles. Is there any good way for me to get the raw content without the <!--more--> removed?
Chances are, by the time your plugin runs, <!--more--> has been converted to <span id="more-1"></span>
This is what I use in my plugin, which injects some markup immediately after the <!--more--> tag:
add_filter('the_content', 'inject_content_filter', 999);
function inject_content_filter($content) {
$myMarkup = "my markup here<br>";
$content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'."\n\n". $myMarkup ."\n\n", $content);
return $content;
}
You can use the follow code:
The !is_single() will avoid display the more link in the View Post page.
add_filter('the_content', 'filter_post_content');
function filter_post_content($content,$post_id='') {
if ($post_id=='') {
global $post;
$post_id = $post->ID;
}
// Check for the "more" tags
$more_pos = strpos($filtered_content, '<!--more-->');
if ($more_pos && !is_single()) {
$filtered_content = substr($filtered_content, 0, $more_pos);
$replace_by = '<a href="' . get_permalink($post_id) . '#more-' . $post_id
. '" class="more-link">Read More <span class="meta-nav">→</span></a>';
$filtered_content = $filtered_content . $replace_by;
}
return $filtered_content;
}
Based on Frank Farmer's answer I solved to add thumbnail photo after the generated more tag (<span id="more-...) in single.php file with this:
// change more tag to post's thumbnail in single.php
add_filter('the_content', function($content)
{
if(has_post_thumbnail())
{
$post_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class'=>'img img-responsive img-thumbnail', 'style'=>'margin-top:5px;'));
$content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'.$post_thumbnail, $content);
}
return $content;
}, 999);

Resources