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

  • Home
  • SEARCH
  • 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 8310907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:37:51+00:00 2026-06-08T19:37:51+00:00

On my site I have one global Javascript file which includes jQuery and code

  • 0

On my site I have one global Javascript file which includes jQuery and code for the drop down menu among other things. Many pages also have custom Javascript for minor page-specific interactions, tables etc.

My current set up on each view is a header.php file, basically covering everything from the doctype through to start of the content, the view file for the specific page, and a footer.php closing out the page.

Currently global.js is linked from the <head>. For performance we should put JS at the very bottom of the page, but I can’t figure out a good way to do this. I could add the full script line for global.js with the custom script block, but that means I must add it on every page, even when there is no other Javascript. Any better way to move the JS right to the bottom?

  • 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-06-08T19:37:52+00:00Added an answer on June 8, 2026 at 7:37 pm

    You could put the custom JS in a regular variable or nowdoc (php 5.3.0+), and then echo the variable along with script tags in the footer if it exists. Nowdoc might be preferable because you can use both double quotes and single quotes in your JS and PHP won’t parse/escape the text.

    someview.php:

    <?php 
    $custom_js = "
    alert('custom js ran');
    ";
    ?>
    
    <?php 
    $custom_js = <<<'CUSTOM_JS'
    alert("custom js ran (i'm in a nowdoc!)");
    CUSTOM_JS;
    ?>
    

    footer.php:

    <?php if(isset($custom_js)) { ?>
        <script><?php echo $custom_js; ?></script>
    <?php } ?>
    

    Edit 2:
    If you don’t want to have the javascript in a string, you could have the javascript in a seperate file and then use PHP’s file_get_contents() to load it into the $custom_js variable as a string.

    Edit:
    This is just an aside, but you might look into using the Carabiner library for loading JS and CSS. It’s an excellent library. It might not necessarily help with your current problem, but if your global.js is quite large, you could split it up and use Carabiner to compress/concatenate on load. I currently use it to select which JS and CSS gets loaded for logged in and logged out users on my current CI project.

    • Carabiner on Github
    • Carabiner on Sparks
    • Carabiner documentation
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WordPress site which have 4 pages with one page is a
I have a site that has 2 DIV tags, one floats left the other
I have one core site which has all the core functions. I then need
I have a global functions.php file containing functions used throughout my site. In terms
I have a fairly busy site which does around 10m views a month. One
I have a site using 2 css : one global, one page specific. By
I have one web site in IIS, and I would like to have version
I have develop one web site in php. I know how to upload it
Hey I have downloaded one of the xml editors from some site. But don't
Post-release, I have made one small change to one form in our development site

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.