使用PHP注入JavaScript


Inject JavaScript using PHP

我想使用PHP加载外部网页,并在显示它之前注入一些JavaScript。

老实说,我根本不知道该怎么做(如果可能的话)。有人吗?

的例子:

$html = file_get_contents($url);
// inject javascript here
echo $html;

你需要简单地构造你的js代码的字符串,如果它是只为该页,并将其添加到一个脚本标签和echo整个字符串。这样的:

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

或者如果它是一个文件包含,然后包含它与脚本标签和echo它,它将在您的页面上可用。这样的:

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

在这种情况下,你的代码结构将变成这样

    $html = file_get_contents($url);
    echo "<script src='path to file'></script>"; 
   // echo "<script>alert('hi');</script>"; //as page script example
    echo $html;

直接回显

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