CodeIgniter Forums

Full Version: Fix for the javascript.php - core library - function external(); Line 637 - 666
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
CodeIgniter Version 2.0.3 / 2.1.0

This is the original code and it fail with the loading of local files..

Here is the logic...

If no file is submitted, load the file path as filename ?!? It's a bit of nonsense..

Logic continue after the display of the original code...

Code:
function external($external_file = '', $relative = FALSE)
{
  if ($external_file !== '')
  {
   $this->_javascript_location = $external_file;
  }
  else
  {
   if ($this->CI->config->item('javascript_location') != '')
   {
    $this->_javascript_location = $this->CI->config->item('javascript_location');
   }
  }

  if ($relative === TRUE OR strncmp($external_file, 'http://', 7) == 0 OR strncmp($external_file, 'https://', 8) == 0)
  {
   $str = $this->_open_script($external_file);
  }
  elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
  {
   $str = $this->_open_script($this->_javascript_location.$external_file);
  }
  else
  {
   $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location);
  }

  $str .= $this->_close_script();
  return $str;
}

Else, it is not an url so open the script tag, load the file(Remember my first note here) and the filename (path submitted turned to filename) is added to filename (Result: filefile).

Confusing :-S

In brief and more simple to understand.. If it is a local file.. Take the filename as path and next add it to the filename... Logical problem here... filename.extfilename.ext


Here is my fix with a few extras and comments. :-)

Code rewritten (6/6/2012), dry version.
Code:
function external($external_file = '', $relative = FALSE, $extension = '') // added extension param.
{
if ($external_file !== '')
{
  $this->_javascript_location = $external_file;
}
else
{
  // nothing submitted, fail
  return false;
}

// does the extension contains the js extension or are we loading something else in javascript tags (ex: script.php)
if($extension !== '') {
  $ext = (strpos($extension, '.') === '') ? '.'.$extension : $extension ;
}
else
{
  // add the .js at the end of file if ext not found else keep the variable empty
  $ext = (strpos($this->_javascript_location, '.js') === '') ? '.js' : '' ;
}

// it's a url
if ($relative === TRUE OR strncmp($external_file, 'http://', 7) == 0 OR strncmp($external_file, 'https://', 8) == 0)
{
  $str = $this->_open_script($external_file.$ext);
}
// it's a secure url
elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
{
  $str = $this->_open_script($this->_javascript_location.$external_file.$ext);
}
// it's a file
elseif ($this->CI->config->item('javascript_location') !== '') // load config values in config.php
{
  $str = $this->_open_script($this->CI->config->item('javascript_location').$this->_javascript_location.$ext);
}
// it's a file and config.php is unset
else
{
  $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$ext);
}

$str .= $this->_close_script();
return $str;
}

Now for the loading.

Code:
$this->load->library('javascript');


// loading with relative url and extension with or without the dot
echo $this->javascript->external('JsFile',FALSE,'txt');
echo $this->javascript->external('script',FALSE,'.php');

// or load it the normal way
echo $this->javascript->external('JsFile.js');

// or just load it the lazy way with defaults
echo $this->javascript->external('JsFile');

Tested and working!
Enjoy!

PS. Sorry for my english, I need more practice since this is not my native language.