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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:45:57+00:00 2026-05-26T17:45:57+00:00

I have an index.php file that loads other php files via this javascript/ajax code:

  • 0

I have an index.php file that loads other php files via this javascript/ajax code:

function AJAX(elementID,url,showStatus){
var httpObject;
if (window.ActiveXObject) {
    httpObject = new ActiveXObject("Microsoft.XMLHTTP");
}
if (window.XMLHttpRequest){
    httpObject =  new XMLHttpRequest();
}
else {
    alert("Your browser does not support AJAX.");       
}

if (httpObject != null) {
    httpObject.onreadystatechange = function() {          
        if (elementID != false){                

            if (httpObject.readyState == 4 && httpObject.status == 200) {                  
                document.getElementById(elementID).innerHTML= httpObject.responseText;  

            } 
        }

    }

    httpObject.open("POST",url,true);
    httpObject.send(null);  
}
}

so for example I would load a file in inxex.php by:

<script>
AJAX("updateThisDiv", "/includes/contentpage.php", false)
</script>

which would paste the contents of “contentpage.php” into the div “updateThisDiv” but now if I have any javascript on “contentpage.php”, it will not run, is there any way to do this?

I have looked at this: http://www.javascriptkit.com/script/script2/ajaxpagefetcher.shtml
but its not exatcly what I was looking for.

I want to be able to update a section of my page without reloading the entire page and javascript must run

  • 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-26T17:45:58+00:00Added an answer on May 26, 2026 at 5:45 pm

    If you want to load Javascript on demand. This can be done by dynamically creating script tag. This pattern illustrated in Stoyan Stefanov book – Javascript Patterns

    This snipped from the book:

    Write a require function. Then call it like this:

    require("extra.js", function () {
        functionDefinedInExtraJS();
    });
    

    Sample require function:

    function require(file, callback) {
    
        var script = document.getElementsByTagName('script')[0],
            newjs = document.createElement('script');
    
        // IE
        newjs.onreadystatechange = function () {
            if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
                callback();
            }
        };
    
        // others
        newjs.onload = function () {
            callback();
        };
        
        newjs.src = file;
        script.parentNode.insertBefore(newjs, script);
    }
    

    Live example found in http://www.jspatterns.com/book/8/ondemand.html

    @Edit: more details specific to your case. I will try to make things simple:

    Create four files:

    • index.php : the test file.
    • code.js : actual code which have Ajax, getJS, getHTML functions
    • content.php : any PHP file that will print pure HTML without any JS
    • content.js : javascript code that you want to run dynamically.

    index.php

    <html>
        <head>
            <script src="code.js"></script>
        </head>
        <body>
            <input type="button" onclick="Ajax('content.php', 'd_html');" value="Fill from content.php"/>
            <div id="d_html"></div>
            <br>
            <input type="button" onclick="Ajax('content.js', 'd_js');" value="Fill from content.js"/>
            <div id="d_js"></div>
        </body>
    </html>
    

    content.php

    <span>Hello, I am dynamic span came from content.php</span>
    

    content.js

    //Ana javascript code you want to run it by Ajax function should go inside this function
    function executeJS(element){
       element.innerHTML = "<span>Hello, I am dynamic span came from content.js</span>";
    }
    

    code.js

    //This function responsible for doing the ajax request for any file that will return pure HTML.
    function getHTML(url, element){
        var i, xhr, activeXids = [
            'MSXML2.XMLHTTP.3.0',
            'MSXML2.XMLHTTP',
            'Microsoft.XMLHTTP'
        ];
    
        if (typeof XMLHttpRequest === "function") { // native XHR
            xhr =  new XMLHttpRequest();        
        } else { // IE before 7
            for (i = 0; i < activeXids.length; i += 1) {
                try {
                    xhr = new ActiveXObject(activeXids[i]);
                    break;
                } catch (e) {}
            }
        }
    
        xhr.onreadystatechange = function () {
            if (xhr.readyState !== 4) {
                return false;
            }
            if (xhr.status !== 200) {
                alert("Error, status code: " + xhr.status);
                return false;
            }
            
            element.innerHTML += xhr.responseText;
        };
    
        xhr.open("GET", url, true); 
        xhr.send("");
    }
    
    //This function will load javascript file on-demand and call executeJS function inside that file.
    function getJS(url, element, cb){
        var newjs = document.createElement('script');
        
        // IE
        newjs.onreadystatechange = function () {
            if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
                cb();
            }
        };
    
        // others
        newjs.onload = function () {
            cb();
        };
        
        newjs.src = url;
        element.appendChild(newjs);
    }
    
    
    //This is same as your function, but now can handle both PHP and JS files
    function Ajax(url, id){
        var element = document.getElementById(id),
            regex = /\.js$/;
        if(!element){
            alert("Invalid ID");
            return false;
        }
    
        if(regex.test(url)){ //If url ends with JS, load using getJS
            getJS(url, element, function(){
                executeJS(element);
            });
        } else {
            getHTML(url, element);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a index.php file that will include several external files: content/templates/id1/template.php content/templates/id2/template.php content/templates/id3/template.php
I have an htaccess file that uses mod_rewrite to redirect /controller to /index.php?controller=%controller% Like
I have 2 php files: index.php (5KB) blob.php (50,000KB - yes, 50mb php file)
I have a php file that loads an article from a db based on
The setup: I have a standard .php file (index.php) that contains two includes, one
I have this ajax_update script that updates file.php every 60 seconds.. Now file.php outputs
I have an index.php file which has to process many different file types. How
i have two files:(localhost/template/) index.php template.php each time when i create an article(an article
I have a live site that includes different php files depending on what page
Let's say we have the following schema: root/ application/ -public/ index.php css/ img/ javascript/

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.