Home > OS >  ASP.NET web app force an iframe to reload
ASP.NET web app force an iframe to reload

Time:11-20

I'm not new to .NET Core development, but I am very new to ASP.NET Web Apps.

I have an Index.cshtml that has the following iframe in it:

    <iframe 
      height="800" 
      width="1300" 
      loading="eager" 
      frameBorder="0" 
      scrolling="no" 
      seamless="seamless" 
      allowtransparency="true" 
      src="https://example.com/?page_id=436" 
      title="My site" />

I need to force this iframe to reload (not from cache) whenever the page gets refreshed. On the web, I found the following suggestion:

iframe.src = "https://example.com/?page_id=436?reload=" (new Date()).getTime();

Here's my question: Where in a ASP.NET web app (using bootstrap) would I put this code to make it refresh my iframe? I'm somewhat familiar with javascript (haven't used it in a while), have very little knowledge of ASP.NET, and almost none of bootstrap. I know html/css pretty well.

I'm confused about where in the code to put this bit of javascript so that it will always run when I refresh the page. And I don't know how to make that code reference my specific iframe.

Thanks!

CodePudding user response:

This bit of code refreshes your iframe. If you want to refresh it when the page is refreshed, you should execute it after the page loads — in a defer script:

<script defer>
  iframe.src = "https://example.com/?page_id=436?reload=" (new Date()).getTime();
</script>

CodePudding user response:

I was able to verify that the , as SNBS suggested, executed. But for some reason, it didn't do what it needed to do. So, then I got an idea! Why not use the C# code instead of javascript, so now my Index.cshtml looks like this:

@page
@model IndexModel
@{
    ViewData["Title"] = "Example";
    string pageUrl = "https://example.com/?page_id=436?reload="   DateTime.Now.ToString();
}

<div id="site-main">
    <div>
        <h1 >Printing some good stuff here</h1>
        <p >And now some description!</p>
    </div>
</div>
<div>
    <iframe id="cgblog" height="800" width="1300" loading="eager" frameBorder="0" scrolling="no" seamless="seamless" allowtransparency="true" src="@pageUrl" title="My site">
    </iframe>

</div>

The key was to define the variable pageUrl and then put that in the iframe's src. This works like a charm!

  • Related