Jump to content

[SOLVED] Insert javascript file dynamically


dfwcomputer

Recommended Posts

I am currently using a content managment system in which I want to modify.The mods will include a number of javascript files, So I would like to add them dynamically (Isuppose that is the right word)

 

I'll give you an example of what I have come up with...

 

In my header.php file I would have the following

 

if (defined('JS_HEAD')) {
	$JSHead = 'includes/' . JS_HEAD;
	if (file_exists($JSHead)) {
		echo '<script type="text/javascript" language="javascript" src="' . $JSHead . '"></script>', "\n";
	}
}

 

and then when a particular module needs a javascript file inserted in the header file I could just call

 

define('JS_HEAD', 'myfile.js')

 

 

Is this the best way to go about it?

Or does anyone have another solution.

 

 

 

Link to comment
Share on other sites

Am i even close lol

 

 

if (defined('JS_HEAD')) {
$JSHead = JS_HEAD;
$JSFile = array($JSHead);
var_dump($JSFile);
foreach ($JSFile as $jsArray) {
		if (file_exists($jsArray)) {
		echo '<script type="text/javascript" language="javascript" src="' . $jsArray . '"></script>'. "\n";
		}
	}
}

Link to comment
Share on other sites

Why not add you'r 'modules' into an array and loop that;

 

<?php
$JSmodules = array('module1.js','module2.js');
foreach($JSmodules as $JSmodule){
if(file_exists($JSmodule)) echo '<script type="text/javascript" language="javascript" src="' . $JSModule . '"></script>'. "\n";
}
?>

Link to comment
Share on other sites

I'm not sure I understand what you're trying to reach rather than including all of the javascript files available from a list (array)

 

yes thats correct but I dont want to have to update the header file everytime with

 

$JSmodules = array('module1.js','module2.js','module3.js');

 

I would just like to define the file when I build the module e.g.

 

define('JS_HEAD', 'module1.js')

 

Then the header file would take all the defined files and insert them.

Link to comment
Share on other sites

mmmm ok I have this in the header file

 

$JSmodules = array();
foreach($JSmodules as $JSmodule){
	if (file_exists($JSmodule)) {
		echo '<script type="text/javascript" language="javascript" src="' . $JSModule . '"></script>'. "\n";
	}
}

 

and I have this in the top of my module

 

$JSmodules[] = 'includes/module2.js';

 

but it does not display it in the source code.

 

If i change

 

$JSmodules = array();

 

To

 

$JSmodules = array('includes/module2.js');

 

it display this in the source code

 

<script type="text/javascript" language="javascript" src=""></script>

 

any ideas on why it wont work

 

 

Link to comment
Share on other sites

When you say;

 

and I have this in the top of my module

 

$JSmodules[] = 'includes/module2.js';

 

but it does not display it in the source code.

 

Do you mean you're putting the relevant link at the top of the page you're trying to include?

Link to comment
Share on other sites

 

Do you mean you're putting the relevant link at the top of the page you're trying to include?

 

I think I understand what your saying and yes.

 

here is a complete test page

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?php
$JSmodules = array();
   foreach($JSmodules as $JSmodule){
      if (file_exists($JSmodule)) {
         echo '<script type="text/javascript" language="javascript" src="' . $JSModule . '"></script>'. "\n";
      }
   }
?>
</head>

<body>

<?php
$JSmodules[] = 'includes/module2.js';
?>
</body>
</html>

Link to comment
Share on other sites

Are you always going to want to include all the javascript files?

 

some files yes, but I will just add them to the core.But some modules will need a js file of there own and it will only need to be loaded when that module is active.Thats why I would like to define it some how in the module file that this particular file is needed and it will be automatically loaded in the header file.Im sure there would be an easy solution as I doubt I would be the first one to come up against this issue.

Link to comment
Share on other sites

If I'm understanding you, you want a method enabling you to add js files to your html <head> after the fact?

 

This is one reasonablky simple method:

 

Your going to need to put your entire output into a buffer, then replace a placeholder with your calls to the js files.

 

I'll try and keep this simple and hope you get the idea.

 

header.php

<?php ob_start(); ?>
<html>
  <head>
  </head>
  <body>

 

footer.php

  </body>
</html>
<?php
$output = ob_get_contents();
if (isset($js) && is_array($js)) {
  $replace = "<head>\n";
  foreach ($js as $jsscript) {
    $replace .= "    <script type=\"text/javascript\" src=\"/js/$jsscript\"></script>\n";
  }
  $output = str_replace('<head>', $replace, $output);
} 
echo $output;
ob_end_flush();
?>

 

main.php

<?php

  include 'header.php';

  // here is the guts of your application doign whatever it does.

  // load some js libs into an array.
  $js = array('foo.js','bar.js','bob.js');

  // carry on with application.

  include 'footer.php'

?>

 

This would produce the following output:

 

<html>
  <head>
    <script type="text/javascript" src="/js/foo.js"></script>
    <script type="text/javascript" src="/js/bar.js"></script>
    <script type="text/javascript" src="/js/bob.js"></script>
  </head>
  <body>
  </body>
</html>

 

Hope it helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.