Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4345108
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:59:25+00:00 2026-05-21T11:59:25+00:00

I am not sure if this general question is even allowed in here but

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:59:26+00:00Added an answer on May 21, 2026 at 11:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I suspect this applies to general ASP.Net too but I am not sure. If
This may be for most languages in general, but I'm not sure. I'm a
Not sure this is a programming question, but we use LaTeX for all our
Not sure if this would be better on Programmers or another Exchange but here
I am not sure if this question is not silly, but I really want
Not sure this is possible, but looking to write a script that would return
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not sure this is possible, but is there a syntax to be used
I am pretty sure this is possible just not sure what is the term
I'm sure this is a fairly trivial problem, but I'm not sure what to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.