0

I am not sure if this general question is even allowed in here but I try it anyways. I am creating a new site (just for fun and to "train" my skills). I am at a point where I have some lines of javascript code, but the code is not needed on every page.

So I have decided to paste ALL javascripts into a php file (javascript.php) and inside that file I have added following lines:

<?php  $site_name = basename($_SERVER['REQUEST_URI'], ".php"); ?>
<?php if ($site_name == "index") { ?>
<script type="text/javascript">
//script 1
</script> 
<?php } elseif ($site_name == "index1") { ?>
<script type="text/javascript">
//script 1
</script> 
<?php } elseif ($site_name == "index2") { ?>
<script type="text/javascript">
//script 2
</script> 
<?php } elseif ($site_name == "index3") { ?>
<script type="text/javascript">
//script 3
</script> 
<?php } ?>


It works fine.
But, as I am a coding newbie, I was wondering if this a smart solution? Could you girls and guys tell me your opionion and why it is smart/dumb to do this?
Thanks!

2 Answers 2

2

If you have to have separate Javascript includes, I wouldn't use an IF, since it can get quite large and unmanageable. You could use a switch (same problem, IMO), or just put the JS into separate files and include according to the $site_name:

http://codepad.org/Ha02Uscr

<?php

$site_name = basename($_SERVER['SCRIPT_NAME'], ".php");

echo "
<script type=\"text/javascript\" src=\"$site_name.js\"></script>
";

?>

Of course, there's other ways of doing it too. You might consider a framework like CodeIgniter, CakePHP, or Drupal to manage your files.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your opinion. I will most probably move the scripts to seperate files and include like you did in your example. I think I wont start using a framework right now, because i think a)php itself is the best framework and b) i would like to code everything from scratch to learn it by doing it.
One more thing: I have read different things about this topic: should I include all script at the bottom of the page (before the body or before the html tag? or in the head section?
The script tags should be either in the HEAD tag, or somewhere within the BODY tag. The reason for putting them in the bottom of the BODY tag is that, if your code references elements within the BODY tag, then you won't have errors. Another option is to use a framework (I use jQuery) and then use $(document).ready (or onDOMReady).
Thanks for you answer! Finally some comprehensible answer. One more thing: if i understood correctly: if i use the document ready tag (i am using jquery as a framework too) than it doesnt matter if the script is in the head or bottom part?
No it doesn't; it gets added to a queue and run at the onDOMReady event.
|
1

I would recommend keeping everything in one javascript tag, and (if the javascript files are large enough) move them to separate files instead of inline. Other than that, I would consider it a better solution than making the browser process javascript it doesn't need. Just remember you did this when the javascript can't find a function ;)

1 Comment

Hi mazzzzz, Thanks for you opionion! I will most probably move them in speerate files as you and Jared suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.