I've condensed my problem into the following sample code:
<?php
if($_GET['ajax']){
echo ' <script type="text/javascript">
alert("test alert");
var newdata="Hello Again!";
$(container).append(newdata);
</script>';
exit();
}
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var container = document.getElementById('testdiv');
var data = 'Hello World' ;
$(container).append(data);
});
function goAjax(){
$.ajax({
url: '/test.php/?ajax=1',
type: "POST",
data: {"xxx":1},
success: function(data){
$("#return").empty();
$("#return").append(data);
}
});
};
</script>
</head>
<body>
<a href="#" onclick="goAjax();">test</a>
<div id="testdiv"></div>
<div id="return"></div>
</body>
</html>
What I'm trying to do is load in the JS script located at the top of the code into the web page and have it execute.
The alert()
executes fine, but I cannot get Hello Again!
to show up. It is not executing that part of the script.
Can someone help to explain why?
Note that the use of a reference defined earlier (container
) is vital to my problem, and a workaround involving a direct reference does not solve things for me.
Thanks!