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 8536963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:47:12+00:00 2026-06-11T10:47:12+00:00

I have the following in the body of a php page: <?php if($foo) :

  • 0

I have the following in the body of a php page:

<?php if($foo) : ?>

    <script>
        js_func();
    </script>

<?php else: ?>

    //Do Something else

<?php endif; ?>

Based on the PHP conditional I either do or do not want to run js_func().

However if I am loading all of my scripts (including the script the defines js_func()) at the bottom of my page this will results in an error.

One possible solution would be to load the external script BEFORE calling js_func() but I understand that for performance reasons I shouldn’t do that.

I could use $(document).ready(function() {}); but this just moves the error as jQuery is also loaded in the footer.

The only other options I can think of is to use window.onload or never call a js function inline. How does everyone else solve this issue?

Many thanks.

EDIT:

@Nile – Im not sure what you mean. Why would I comment out code that I want to execute?
@haynar1658 – I don’t want to execute JS in the else scenario.
@Matthew Blancarte – Understood. That leads to my question, what’s the best way to make sure that the js I need loads before that function is instantiated? Include the script before it? Use window.onload? etc.

  • 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-11T10:47:14+00:00Added an answer on June 11, 2026 at 10:47 am

    I think you are making a rod for your own back. Depend on the question you described, you want to put all the function definition script block after the place where them being called. It’s Impossible!
    If you indeed need to do this, does this can help? :

    <script>    
        var fns = []; /* use fns to keep all the js code  
                         which call the functions defined after. */  
    </script>
    
    
    <script>
        //wrapp your code in a function and then push it into fns.   
        fns.push(function(){  
            js_func();  
        }) 
    </script>
    
    
    //script tags for loading your function  definition js script.  
    <script src="path/to/jquery-any-version.js"></script>  
    <script src="path/to/other-libraries.js"></script>
    
    
    <script>
        //after your definition js scripts are loaded ,  call all functions in fns  
        for(var i=0, len=fns.length; i<len; i++){  
            var fn = fns[i];  
            fn.apply(this, []/* arguments that provided  as an array */);  
        }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page, test.php, with the following code: <html> <body> <form> <script type=text/javascript>
I have the following code inside the body of my index.php page: echo Begin
Imagine for a second you have the following php based page served via apache
I have the following jQuery code: $(body.page-calendar-practices-2012-05 #content-header h1#title) which works fine. I can
I have the following jquery code: $(document).ready(function() { $(body[class*=page-calendar-practices] #content-header h1#title).after(<div id='athletics_practice-schedule'><div id='inner-title'><a href='www.example.com'
I have the following elements in a HTML page: <body> <div style=height:100%; width:100%> <div
I have the following code that works fine in IE: <HTML> <BODY> <script language=JavaScript>
I have the following page, which works with MySQL, PHP and AJAX if I
I have the following code: <html> <head> <title><?php echo $GLOBALS['L']['title']; ?></title> </head> <body> <ul
I have the following script: EDIT: <?php session_start(); //connect to database function connect() {

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.