0

How do I include php link in page using Javascript

Using only Javascript

I had a code but forgotten it

ex

This in php

 <? php
 file_get_contents('www.xxxxxxxxx.com/icn.php); 
  ?>

I want in Javascript

2 Answers2

0

Since you are including an external domain in the precess (not using a relative path), the request will probably be cancelled due to the same origin policy browser implements to avoid cross-site forgery. In that case, you may want to have a PHP file on your website that just proxies your request to the actual site you want to load and use AJAX to your local PHP script.

Your Javascript <> PHP script on the same domain <> www.xxxxxxxxx.com/icn.php

If www.xxxxxxxxx.com happens to be your own website, you can just do a regular AJAX call to "icn.php". To do AJAX calls, you may want to read about XMLHttpRequest or use a library that does the job for you, like jQuery.ajax().

If you really absolutely need to load data from an external domain purely on client side, you can either use hacks like JSONP or an invisible <iframe>, listen to its onload event and get its content. But be careful, these usually requires the external site to be compatible with these kind of requests (not a big deal if you manage both ends).

Community
  • 1
  • 1
Max-P
  • 338
  • 2
  • 11
-2

fun.php

< ?php

echo 'hello World';

?>

function foo() {

var i = < ?php include('fun.php');?>

console.log( i );

}