Get title from IFrame document

后端 未结 2 1199
忘掉有多难
忘掉有多难 2020-12-19 14:58

This is my code:

相关标签:
2条回答
  • 2020-12-19 15:14

    You need to check that the iframe is loaded.

    <div id="main-content">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0" onload="document.title=parent.frames['cframe'].document.title;">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
    <script>
        $( "iframe" ).on('load',function() {
            document.title = document.getElementById("main-content-iframe").contentDocument.title;
        });
    </script>
    

    0 讨论(0)
  • 2020-12-19 15:30

    Try code maybe can help you

    HTML code:
    <div id="main-content">   
        <iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
    </div> 
    
    jQuery code:
    alert($('#main-content-iframe').contents().find("title").text());
    

    Note: Make sure home.html come from the same domain as your main page.

    0 讨论(0)
提交回复
热议问题