1

I would like to load external webpage using PHP and to inject some JavaScript to it before displaying it.

To be honest, I have no idea at all how to do it (if it is possible). Somebody?

Example:

$html = file_get_contents($url);
// inject javascript here
echo $html;
1
  • I would like to inject some JavaScript before displaying it? So like a XSS attack? Commented Oct 18, 2015 at 16:47

2 Answers 2

3

you need to simply construct the string of your js code if it is for that page only and add it inside a script tag and echo the entire string. like this:

echo "<script>alert('hi');</script>"; //as page script example

Or if it is a file include then include it properly with script tag and echo it and it will be available on your page. like this:

echo "<script src='path to file'></script>"; 

In that case your code structure will become like this

    $html = file_get_contents($url);
    echo "<script src='path to file'></script>"; 
   // echo "<script>alert('hi');</script>"; //as page script example
    echo $html;
Sign up to request clarification or add additional context in comments.

3 Comments

Please explain your answer as it has been flagged as low quality.
@RohitGupta where exactly can we see these flags.
I don't know how to find it again. But I was reviewing the Low Quality Queue when I came across it. And I was giving you a heads up, rather than recommending deletion.
1

simply echo it

$html = file_get_contents($url);
echo "your  javascript here"; 
echo $html;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.