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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:40+00:00 2026-05-26T08:25:40+00:00

I am loading a .php file into a div I created. I can successfully

  • 0

I am loading a .php file into a div I created.
I can successfully load the file into the div and everything works except javascript.
When i test the file in my browser, the javascript works, but not when it’s injected into my div.

index.php

<?php
$filename = $_GET["filename"];  
if($filename != ""){        
    $fileData = file_get_contents($filename);
    $fileData = trim($fileData);
    $fileData = str_replace("\n", "", $fileData);
    $fileData = str_replace("\r", "", $fileData);
    echo $fileData;
    die;
}?>

<body>
<script>
function LoadFile(filename, javascriptDiv){
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            javascriptDiv.innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET","index.php?filename="+filename,true);
    xmlhttp.send();
}

var javascriptDiv = document.createElement('div');
javascriptDiv.setAttribute('id', 'javascriptDiv');
javascriptDiv.style.position = 'absolute';  
javascriptDiv.style.top = 50;
javascriptDiv.style.left = 50;
javascriptDiv.style.height = 200;
javascriptDiv.style.width = 300;
javascriptDiv.style.background = '#CCCCCC'; //so we can see that the DIV was created

LoadFile('http://127.0.0.1/Debug/test.php', javascriptDiv);

document.body.appendChild(javascriptDiv); //Display the Window
</script>
</body>

test.php <–the file i’m loading into the div

Plain text works
<?php echo "<br>php works <br>"; ?>
<a>html works</a><br>

<script>
function Test(){
    alert('javascript works');
}
</script>

<input type="button" value="Test Javascript" onclick="Test()"/>

Here’s what it looks like on my site.
index.php

And here is a direct link to the test.php file
test.php

I need to get this working without altering the test.php file.

  • 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-26T08:25:40+00:00Added an answer on May 26, 2026 at 8:25 am

    No, you can’t evaluate tags with lazy loading.

    Generally speaking, I think the idea of allowing users to run custom javascript is a door open to exploits. I don’t know exactly what you are trying to achieve and why, but you’d better create a set of functions yourself and publish a simple public API (for example, to get a lightbox, add a ‘lightbox’ class to your element, or that kind of thing).

    Alternatively, you could fetch the content of the tag and run an eval(). But again, it’s dangerous.

    This page explains quite well the different methods you can use to achieve this. Of course, all assume you are running your own code and not some user code.

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

Sidebar

Related Questions

Possible Duplicate: Problem when loading php file into variable (Load result of php code
i'm loading a php file with jquery .load. with load it just loads my
Using .load , I'm loading the following content into #step_2 div: <div id="step_2"> Select
i have a problem with loading .html pages (with JavaScript inside) into a div
I am working on code re-factoring of configuration file loading part in PHP. Earlier
After loading a PHP template (using jQuery's load function), this simple script won't make
I am loading HTML into DOM and then querying it using XPath in PHP.
I was wondering whether there is a simple PHP library to test the loading
I have a jquery function loading a php file which will output some data
I'm having issues with loading the user's information into Flash from a PHP script.

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.