最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Load external resources only on button click - Stack Overflow

programmeradmin2浏览0评论

I would like to have a "Load More" button that displays all the content of a div tag when clicked. It's important that all external resources within this content (such as images, JavaScripts, iframes, etc.) are fetched by the browser not immediately on each page load, but only after the user clicks the button.

I believe this can be achieved by first removing the content div from the DOM and then adding it back on button click, but I am not sure how to do that. Can someone please help me? Thank you!

I'd like to mention that the content div can be quite large (several kilobytes), so it needs to be located in the HTML code (as shown in the code below), rather than in the JS code. It will also be dynamic, constantly changing.

Alex

function load_more() {
  document.getElementById('content_div').style.display = 'block';
}
<html>
  <body>
    Content here...<br><br>
    <div style='display: inline-block; border: 1px solid black; padding: 10px; cursor: pointer' onclick='load_more()'>Load More</div><br><br>
    <div id='content_div' style='display: none'>
      More dynamic content here, including external images, scripts, and even iframes. These should NOT be automatically retrieved by the browser immediately on each page load, as they currently are with this code alone. Instead, they should only be fetched after the button above is pressed. So, if the user loads the page but does NOT click the button, no external resources will be retrieved by the browser.
    </div>
  </body>
</html>
发布评论

评论列表(0)

  1. 暂无评论