Move inline script into external js file

Hi there,

the logic as outlined in the attached file is working however I would like to move the inline script (line 8 to 354) into an external js file. only how ?

if I move the above to dtree.js, adjust function dTree(objName) for function dTree(objName, path) and adjust $path for path under icons and use
$jsfile = $baselink.$this->template.‘/js/dtree.js’;
<script type=“text/javascript” src=“$jsfile”></script>
how am I to pass $path to the script?

I am sorry if this is majorly basic but I’m only starting with learning js

could somebody please help me figure how to achieve that?

<script type="text/javascript" src="tree.php?path="<?php=$path ?>"></script>

Note, the JS file is named php. The very first line of you JS file should have this,
<?php header(“Content-type: application/x-javascript”);?>

Right after that line you will want.
$path = $_GET[‘path’];

Since you are using Heredoc, I have no idea if (how) this will work. Another workaround would be to use an include.

thanks for getting back to me! :slight_smile:

unfortunately I’m not getting it to work.

I created a new file called tree.php with the following snippet at the very top:

<?php header("Content-type: application/x-javascript");?>
$path = $_GET['path'];

followed by the lines 25-378 of the attached file

in the attached file I have removed lines 25-378 and inserted above the heredoc snippet:

$js = '<script type="text/javascript" src="$path/tree.php?path="$path"></script>';

while my IDE does not show any syntax errors and there are also no errors displayed on user side the tree is not being displayed anymore

would you have any advice what I’m overlooking?

hmmm! the second snippet in my second post is wrong. should be:

$baselink = $this->interface->getCfg('live_site').'/components/com_component/templates/';
$jsfile = $baselink.$config->template.'/js/tree.php';
$js = '<script type="text/javascript" src="$jsfile?path=$path"></script>';

that however still doesn’t make it work