简体   繁体   中英

Javascript is not loading when I load the content of external website

I have a page where I am loading external site content through Jquery Post method to my PHP file (due to cross site issue) which looks like this.(back.php)

$url = $_POST['url'];
echo file_get_contents($url);

And My HTML code looks like this

    $.post ("back.php",
    {
        url : "http://www.ralphlauren.com/product/index.jsp?productId=2130294&cp=1760781.1760809&ab=ln_men_cs1_polos&parentPage=family"
    }
    ,
    function (data)
    {

        document.getElementById ("output").innerHTML =  data;
    }
);

The site content is loading fine, but the script is not loading, because of that I am getting error while changing any options which should execute the script.

I tried different methods but no use.

How Can I achieve to load the script also.

EDIT It looks like my question was not clear.

The issue is, the content along with script of the given URL is loading in my page. The external URL contains some embedded scripts which is not executing.

Here is an example of the external site

<html>
<body>
Hello
<script>
alert("This is some message"); 
</script>
</body>
</html>

Now if we run this page directly in browser, it shows the text "Hello" as well as alert message, however when I load this file though the above method (POST/Jquery), it is showing "Hello" but not displaying the alert message (means, not executing the javascript).

Please help me to execute that script.

您应该在执行时使用$(function() { })加载js!

Loading HTML with elements into the page is not very stable. This will not work cross-browser cause some browsers don't run the onload in the external page (fetched with ajax).

So don't do this, run the javascript you need in the callback op you $.post .

edit see also this

Not sure why yours is not working but, try this, seems to work perfectly:

<?php 
if($_SERVER['REQUEST_METHOD']=='POST' && $_POST['url']){
    header('Content-Type: text/html');
    echo file_get_contents($_POST['url']);
    die;
}
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" charset="utf-8"></script>

<script charset="utf-8" type="text/javascript">
$(function() {
    $.post ("back.php",{
        url : "http://www.ralphlauren.com/product/index.jsp?productId=2130294&cp=1760781.1760809&ab=ln_men_cs1_polos&parentPage=family"
    },function (data){
        document.getElementById ("output").innerHTML =  data;
    });
});
</script>

</head>
<body>

<div id="output"></div>

</body>
</html>

Error may be caused for following reasons:

  • May be you forget to load jQuery library.

  • You forget wrap you code within DOM ready ie. $(function() { })

  • If you are trying to retrieve data from different domain, then you should try with jsonp type request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM