Reputation: 440
Is it possible to include a file in a file requested by AJAX ? Is it possible to run a php or JavaScript script in a file requested by AJAX ?
this code works in the index.php file
<div id="test">
<script>
$("#test").load("web_elements/nihongo/price_list_kumagai.inc");
</script>
</div> <!--<div id="test"> -->
When I try the same code in a file that is requested by AJAX it doesn't include the file. In the included file I seem not to be able to run any script (not JavaScript, not php) HTML gets parsed correctly.
When I request this file by AJAX
<h3>Price list</h3>
<script>
alert( "You are running jQuery version: " + $.fn.jquery );
</script>
<p>
幼稚園<br>
</p>
the HTML shows normally but everything within the tags is ignored.
Navigation
<a href="#" onclick="loadXMLDoc(language, '/home.inc')">ホーム</a>
LoadXMLDoc function
<script>
function loadXMLDoc(language, pageString)
{
var xmlhttp;
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)
{
document.getElementById("page_content").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",language+pageString, true);
xmlhttp.send();
}
</script>
the LoadXMLDoc function was before I used JQuery. Maybe I should replace it with load() from the JQuery library.
Upvotes: 1
Views: 93
Reputation: 10093
You can include external js files using jQuery.getScript()
Load a JavaScript file from the server using a GET HTTP request, then execute it.
This is a shorthand Ajax function, which is equivalent to:
$.ajax({
url: url,
dataType: "script",
success: success
});
You can also use .load()
method
your code is working properly and load scripts when I remove
document.getElementById("test").innerHTML=language;
line:
price_list_kumagai.inc
<body>
<h3>Price list</h3>
<script>
alert( "You are running jQuery version: " + $.fn.jquery );
</script>
<p>
幼稚園<br>
</p>
</body>
ajax load
<div id="test">
<script>
$("#test").load("web_elements/nihongo/price_list_kumagai.php");
</script>
</div>
Upvotes: 1