0

I want to add more than one site ,, i'm try to rewrite the code and change link " req.open" and noting is done. any help ? i'm Essential at JavaScript. thanks

<html>
<head>
    <title>Testing Purposes</title>
    <script lang="javascript">
        function ready() {
            var hell = document.getElementById('hell');
            var req = new XMLHttpRequest();
            req.open('GET', 'load.php?http://www.140online.com/product/25965/%20%D8%AA%D9%82%D8%B7%D9%8A%D8%B9%20%D9%84%D9%8A%D8%B2%D8%B1', false);


            req.send(null);
            if(req.status == 200) {
                var html_str = req.responseText;
                var doc = document.createElement('html');
                doc.innerHTML = html_str;
                var divs = doc.getElementsByClassName('row-fluid');
                for (i = 0; i < divs.length; i++) {
                    hell.innerHTML = hell.innerHTML +
                                     "<h2>Found:</h2>" +
                                     divs[i].innerText +
                                     "<br />";
                }
            } else {
                alert("Failed to load the page!");
            }
        }
    </script>
</head>
<body onload="ready();">
    <div id='hell'>
    </div>
</body>

Ahmedsaber
  • 19
  • 1
  • 6
  • Use `load` event of `XMLHttpRequest` to process response from server – guest271314 Jul 23 '16 at 15:44
  • I can see you wrong at this line: `req.open('GET', 'load.php?http://www.140online.com/product/25965/%20%D8%AA%D9%82%D8%B7%D9%8A%D8%B9%20%D9%84%D9%8A%D8%B2%D8%B1', false);` It should be `req.open('GET', 'load.php?page=http://www.140online.com/product/25965/%20%D8%AA%D9%82%D8%B7%D9%8A%D8%B9%20%D9%84%D9%8A%D8%B2%D8%B1', false);` – Quynh Nguyen Jul 23 '16 at 15:49
  • See http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/ – guest271314 Jul 23 '16 at 17:06

1 Answers1

0

Use this code

<html>
<head>
    <title>Testing Purposes</title>
    <script lang="javascript">
        function ready() {
            var hell = document.getElementById('hell');
            var req = new XMLHttpRequest();
            req.open('GET', 'jsOnChange.php?page=http://www.140online.com/product/25965/%20%D8%AA%D9%82%D8%B7%D9%8A%D8%B9%20%D9%84%D9%8A%D8%B2%D8%B1', false);


            req.send(null);
            if(req.status == 200) {
                var html_str = req.responseText;
                document.innerHTML = html_str;
                var divs = document.getElementsByClassName('row-fluid');
                alert(divs.length);
                for (i = 0; i < divs.length; i++) {
                    alert('ok');
                    hell.innerHTML += "<h2>Found:</h2>" +
                       divs[i].innerText +
                       "<br />";
                }
            } else {
                alert("Failed to load the page!");
            }
        }
    </script>
</head>
<body onload="ready();">
    <div id='hell'>
    </div>
    <hr>
    <div class="row-fluid">
    Test 1
    </div>
    <div class="row-fluid">
    Test 2
    </div>
</body>
</html>

My result

enter image description here

Quynh Nguyen
  • 2,959
  • 2
  • 13
  • 27