Get HTML code using JavaScript with a URL

前端 未结 5 1351
鱼传尺愫
鱼传尺愫 2020-12-01 14:31

I am trying to get the source code of HTML by using an XMLHttpRequest with a URL. How can I do that?

I am new to programming and I am not too sure how can I do it wit

相关标签:
5条回答
  • 2020-12-01 14:42

    Use jQuery:

    $.ajax({ url: 'your-url', success: function(data) { alert(data); } });
    

    This data is your HTML.

    Without jQuery (just JavaScript):

    function makeHttpObject() {
      try {return new XMLHttpRequest();}
      catch (error) {}
      try {return new ActiveXObject("Msxml2.XMLHTTP");}
      catch (error) {}
      try {return new ActiveXObject("Microsoft.XMLHTTP");}
      catch (error) {}
    
      throw new Error("Could not create HTTP request object.");
    }
    
    var request = makeHttpObject();
    request.open("GET", "your_url", true);
    request.send(null);
    request.onreadystatechange = function() {
      if (request.readyState == 4)
        alert(request.responseText);
    };
    
    0 讨论(0)
  • 2020-12-01 14:43

    You can use fetch to do that:

    fetch('some_url')
        .then(function (response) {
            switch (response.status) {
                // status "OK"
                case 200:
                    return response.text();
                // status "Not Found"
                case 404:
                    throw response;
            }
        })
        .then(function (template) {
            console.log(template);
        })
        .catch(function (response) {
            // "Not Found"
            console.log(response.statusText);
        });
    

    Asynchronous with arrow function version:

    (async () => {
        var response = await fetch('some_url');
        switch (response.status) {
            // status "OK"
            case 200:
                var template = await response.text();
    
                console.log(template);
                break;
            // status "Not Found"
            case 404:
                console.log('Not Found');
                break;
        }
    })();
    
    0 讨论(0)
  • 2020-12-01 14:45

    Edit: doesnt work yet...

    Add this to your JS:

    var src = fetch('https://page.com')
    

    It saves the source of page.com to variable 'src'

    0 讨论(0)
  • 2020-12-01 14:51

    For an external (cross-site) solution, you can use: Get contents of a link tag with JavaScript - not CSS

    It uses $.ajax() function, so it includes jquery.

    0 讨论(0)
  • 2020-12-01 14:59

    There is a tutorial on how to use Ajax here: https://www.w3schools.com/xml/ajax_intro.asp

    This is an example code taken from that tutorial:

    <html>
    
    <head>
        <script type="text/javascript">
            function loadXMLDoc()
            {
                var xmlhttp;
                if (window.XMLHttpRequest)
                {
                  // Code for Internet Explorer 7+, Firefox, Chrome, Opera, and Safari
                  xmlhttp = new XMLHttpRequest();
                }
                else
                {
                    // Code for Internet Explorer 6 and Internet Explorer 5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET", "ajax_info.txt", true);
                xmlhttp.send();
            }
        </script>
    </head>
    
    <body>
        <div id="myDiv"><h2>Let AJAX change this text</h2></div>
        <button type="button" onclick="loadXMLDoc()">Change Content</button>
    </body>
    
    </html>
    
    0 讨论(0)
提交回复
热议问题