0

I have a menu using IFrame Wrapper so that I can link to my own coded php page. Inside the php file is just listing down the data on table and I have search box so that user can filter the data onkeyup. The javascript works if I put the javascript inside the html.

<html>
<script>
function doSearch() {
var searchtext = document.getElementById('searchTerm').value;
var targetTable = document.getElementById('table');
var targetTableColCount;

//Loop through table rows
for (var rowIndex = 0; rowIndex < targetTable.rows.length; rowIndex++) {
    var rowData = '';

    //Get column count from header row
    if (rowIndex == 0) {
       targetTableColCount = targetTable.rows.item(rowIndex).cells.length;
       continue; //do not execute further code for header row.
    }

    //Process data rows. (rowIndex >= 1)
    for (var colIndex = 0; colIndex < targetTableColCount; colIndex++) {
        rowData += targetTable.rows.item(rowIndex).cells.item(colIndex).textContent;
    }

    //If search term is not found in row data
    //then hide the row, else show
    if (rowData.indexOf(searchtext) == -1)
        targetTable.rows.item(rowIndex).style.display = 'none';
    else
        targetTable.rows.item(rowIndex).style.display = 'table-row';
   }
}
</script>
<body>
<form name="searchbox" id="searchbox" method="POST" action="#">
 <input type="text" id="searchTerm" placeholder=" search.." onkeyup="doSearch()" />
</form>
<?php
 if(empty($folderlist))
  {
   echo "<br>";
   echo "<p style='color:red;'>You don't have any acccessible folders.</p>";
  }
 else {
 ?>
<table id="table">
<thead>
<tr>
    <th>No. </th>
    <th>Folders </th>
</tr>
</thead>
<tbody>
<?php for($a = 0; $a < count($folderlist); $a++){ ?>
<tr>
    <td>
    <?php echo $a+1; ?>.
    </td>
    <td>
        <a href="index.php?option=com_content&view=article&<?php echo $folderlist[$a]->getPath(); ?>" target="_top" ><?php echo $folderlist[$a]->getDesc(); ?><a>
    </td>
</tr>
</tbody>
  <?php }
 echo "</table>";
 }
?>
</body>
</html>

But I want to use external filter.js with the same javascript code instead of using internal js file like html file above. I declare below code on top of the html file but it does not work.

  define( '_JEXEC', 1 );
  define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
  define( 'DS', DIRECTORY_SEPARATOR );
  require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
  require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

  $mainframe =& JFactory::getApplication('site');
  $mainframe->initialise();
  $doc = JFactory::getDocument();
  $doc->addScript(JURI::root().'filter.js');

I put the filter.js on the root folder in joomla.

  • 1
    $doc->addScript() includes a Javascript file in head tags. You can't do this outside of Joomla. If you need to include a JS file, simply use <script src="path/to/file"> like you would in HTML – Lodder Oct 3 '15 at 19:35
  • silly me, haha, it works as you suggested, thanks!!! I have another question tho, I realised the js code filter with sensitive case, if I put toLowerCase() in this line (rowData.indexOf(searchtext) == -1), it filters the list without case sensitive, but once I put capital letters, the results are zero, is there a way that I can filter both insensitive n sensitive case? – Annie Oct 4 '15 at 7:46
  • I'd suggest asking a new question on Stackoverflow for that ;) – Lodder Oct 4 '15 at 10:24
2

First of all, you are trying to add code without following Joomla! practices. Then, it is going to be difficult to achieve what your are looking for.

  • For instance, Javascript code is generally located in /media folder.
  • Also, a PHP file without "defined('_JEXEC') or die();" is a bad idea, not following the general security rule to prevent direct access to internal PHP files.
  • Finally, htaccess has several rules to block direct access to certain files.

To add raw code, it is better to install an extension following a controlled way. Please, check this JED category: Coding & Scripts Integration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.