• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Run JavaScript Function Remotely with PHP

If I have a PHP script that loads the content of an external HTML page, for instance:
fopen("http://www.example.com", "r");

Is it possible to execute JavaScript commands that exist on that page? For example, if a button exists on that page like:
<a href="" onclick="javascript...">Link</a>

Could I run that JavaScript from my PHP script?
1 Solution
PHP is server side, JS is client side. You cannot run JS directly in PHP.

Yes its possible to run the java script which is available in the remote server  from PHP. But the content of remote URL should be printed before calling javascript function either using fopen() or file_get_content()

echo file_get_content("http://www.example.com");

If the remote script is is having only JS code then u can directly use  URL in the  'SRC'  attribute of script tag to load the JS function in to the current scope.

<script src="http://www.example.com" > </src>

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now