简体   繁体   中英

How to add result of html page which contains jquery scripts in iframe

I'm getting result of html page which contains Jquery scripts. I'm loading this html in iframe and the jquery is not loaded

$("#getQesAnsButton").click(function() {
    $.get("/getQesAns", function(data, status) {
        $("#Qs").contents().find("body").html(data);
        //$("#Qs").contents().find("html").html(data);
    });
});

The html I'm getting (in 'data') looks:

<html>

<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#buttonSubmit").click(function() {
                console.log("Button Clicked");
            });
        })
    </script>
</head>

<body>
    <form id="formQues1" action="">
        <legend> Q# 1 </legend>
        <input type="radio" name="rad" value="1"> Q1A1
        <br>
        <input type="radio" name="rad" value="2"> Q1A2
        <br> </form>
    <form id="formQues2" action="">
        <legend> Q# 2 </legend>
        <input type="radio" name="rad" value="1"> Q2A1
        <br>
        <input type="radio" name="rad" value="2"> Q2A2
        <br> </form>
    <button id="buttonSubmit"> Submit </button>
</body>

</html>
  • As you can see I have tried to load the iframe with find("html") but I'm getting same results
  • if it's matter the main page (the page which contains the iframe I'm trying to load to) contains JQuery scripts in his head.

How can I load the html page I'm getting in 'data' varable (which contains JQuery scripts in his head) into iframe and the scripts will be loaded ?

The tricky part is that you should get the response as string replace every script tag into something else before using it check my answer to this question here: https://stackoverflow.com/a/7002297/427622

Note: when you append the string to the iframe using JQuery it will first convert the string into JQuery object this will force the JQuery to evaluate all the JavaScript tags witch will make it evaluated in the main page before it is even appended to the iframe

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