简体   繁体   中英

How can we get the variable value from script tag of HTML response using javascirpt?

I would like to get the value of g_listData from the following HTML response (Received this HTML response from FetchAPI request)

<html>
    <body>
    <script type="text/javascript">......</script>
    <script type="text/javascript">......</script>

    <script type="text/javascript">

       var g_deferData= "testdata";

       var g_listData = {"wpq":"","Templates":{},"ListData":{ "Row" : [
           {
            "ID": "85018",
            "FileLeafRef": "read only"
           },
           {
           "ID": "85091",
           "FileLeafRef": "completed"
           }
         ]}
       }

    </script>
    </body>
 </html>

You can try JSON.parse

 const data = `<html> <body> <script type="text/javascript"> var g\_deferData= "testdata"; var g_listData = {"wpq":"","Templates":{},"ListData":{ "Row": [ { "ID": "85018", "FileLeafRef": "read only" }, { "ID": "85091", "FileLeafRef": "completed" } ]} } <\/script> </body> </html>` const div = document.createElement("div") div.innerHTML=data; let g_listData = div.querySelector("script").textContent.split("g_listData =")[1] console.log(JSON.parse(g_listData))

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