I have a php page,say demo.php and i am calling this page using <script src="demo.php"></script>. I need to know whether it is possible to load another page using javascript.
I tried to include a javascript function ,but the required page is not loading.Please find me a solution.
-
I have a php page,say demo.php and i am calling this page using <script src="demp.php"></script>user2307392– user23073922013-07-26 10:07:01 +00:00Commented Jul 26, 2013 at 10:07
-
1Tell us a bit more about what the included page should do. I think you want to use ajax.machineaddict– machineaddict2013-07-26 10:09:35 +00:00Commented Jul 26, 2013 at 10:09
-
2why you are calling server side page via client side coding ?Muhammad– Muhammad2013-07-26 10:11:13 +00:00Commented Jul 26, 2013 at 10:11
-
if i understand you correctly, you are confusing server side and client side... if you want demo.php always included, just include it when you build your site via a php include, if you need it dynamical, as a result of a user action, you need to make an ajax call to request it from the server. jQuery &co have easy to use ajax functionscypherabe– cypherabe2013-07-26 10:24:10 +00:00Commented Jul 26, 2013 at 10:24
-
1@MarinSagovac definitely. But he is trying to load a php file via javascript source.Ihor Patychenko– Ihor Patychenko2013-07-26 10:40:19 +00:00Commented Jul 26, 2013 at 10:40
5 Answers
write method and save. and in file like filename.js
<head>
<script type="text/javascript" src="filename.js"></script>
<head/>
or use
<script type="text/php" src="demo.php"></script>
or
$(document).ready(function(){
$('#div').load('demo.php');
});
you can also use ajax. like
$.ajax({
type: "POST",
url: "demo.php",
data: { name: name, email: email }
,
success: function(msg) {
// alert(msg);
}
});
Hope it will help you.
2 Comments
demo.php this opens a lot of potential.If you want to load another page in the current page, use ajax with jQuery (simple and cross-browser).
Create to javascript files (*.js):
- "jquery.min.js" and copy/ paste [this][2] in.
- "actions.js"
In actions.js :
jQuery(function($){
$.ajax({
url: "demo.php"
})
.done(function(data){
$("body").append(data) // add the loaded page as html at the body end
})
.fail(function(){
console.log("fail to load demo.php");
});
});
Comments
Skip this answer if you don't want like this, but this is using PHP as example. Someone need this example.
In PHP include using Javascript code:
<?php echo '<script type="text/javascript" src="somejavscript.js" />'
Check is really your javascript code:
Or check it like this:
<head>
<?php
$javascript_file = "somedir/somejavscript.js";
if (file_exists($javascript_file))
{
echo '<script type="text/javascript" src="'.$javascript_file.'" />' ?>
}
else
{
die('file not found: '.$javascript_file);
}
?>
</head>
Note, if die() functions call it will stop script and show message error! Use it only for testing.
3 Comments
According to comments, I'm not sure, what are you looking for. I assume, if you want to load another page, that means you want to load another HTML page, thus it is called redirect:
Try modifying the window.location property from javascript. window.location.href = "demo.php";
Or more precisely: window.top.location.href = "demo.php";
But if you want to dynamically load a script file, than you can use the jQuery load function as mentioned in the comments, or use 3 lines of javascript:
var s = document.createElement("script"); // create an empty script tag
s.src = "demp.php"; // specify source
document.getElementsByTagName("head")[0].appendChild(s); // append it to the head tag
// Optionally you can delete the new script tag from the DOM, because the executed code will remain in the memory:
s.parentNode.removeChild(s);
Hope this helps.
Also, if you want to call PHP functions from javascript( because I read that too in comments ), than look for the PHP library: XAJAX
4 Comments
If you use jQuery you can just call
$.getScript('other_file.js', function() {
// script loaded
});
without jQuery you can append script in head
var script = document.createElement('script');
script.setAttribute('src', 'other_file.js');
document.getElementsByTagName('head')[0].appendChild(script);
EDIT: if that file is not javascript inside php then to call it just use normal ajax. Using jQuery it will be:
$.get('script.php', function(result) {
// do something with output from a script
});