The Iframe fails to load initially, but displays correctly upon manual refresh

Currently, I am working in an environment using Asp.net/C# and Visual Studio 2013 to develop a web application.

I have created a page (aspx) with a single iframe on it, along with a JavaScript method that utilizes jQuery to inject HTML into that frame.

<asp:Content runat="server" ID="MainSectionContent" ContentPlaceHolderID="MainSectionContentPlaceHolder">
    <section runat="server" id="FrameSection" clientidmode="Static">
        <asp:ContentPlaceHolder runat="server" ID="FrameSectionContentPlaceHolder" />
        <iframe id="FrameContentFrame" name="FrameContentFrame" src="about:blank" seamless="seamless" onload=" loadFrameContent(); "></iframe>
    </section>

    <script type="text/javascript">
        //<![CDATA[
        function loadFrameContent() {
            jQuery('#FrameContentFrame').contents().find('html').html('<%= FrameContent %>');
        }
        //]]>
    </script>
</asp:Content>

In the code behind, I retrieve a lengthy string from a server and save it in the property FrameContent. This string contains valid HTML markup.

The expected outcome is that upon loading, the JavaScript function will execute, filling the iframe with HTML content and displaying the string to the user.

However, what actually occurs is that nothing is displayed initially. A JavaScript error stating "the function loadFrameContent is undefined" pops up. Strangely, if I view the page source in my browser, the function is clearly visible. When I right-click on the empty frame area and select "reload frame" (in Google Chrome), only then does the HTML content appear.

To clarify: The HTML string and frame are correct, as evidenced by manual reloading showing the content properly. Despite this, the frame fails to display the HTML automatically upon onload, requiring a manual reload for visibility. Checking the page source confirms the presence of both the JavaScript function and the populated FrameContent property containing the accurate HTML string.

Answer №1

It appears to be a timing issue at play here. The blank iframe finishes loading before the parent page, causing the loadFrameContent method to be called before the parent page has completed parsing its DOM. To resolve this, remove the iframe onload event and instead add the following script:

<script type="text/javascript">
    //<![CDATA[
    function loadFrameContent() {
        jQuery('#FrameContentFrame').contents().find('html').html('<%= FrameContent %>');     //FrameContent is a C# property containing valid HTML
    }

    $(loadFrameContent);

    // If using an older version of jQuery
    //$(document).ready(loadFrameContent);
    //]]>
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the functionality of CRUD operations through selenium webdriver testing

My goal is to run tests on a web application that involve creating, editing, and deleting entries from a database. The issue I'm facing is that the tests are interdependent, meaning I have to create an entry before editing or deleting it. Is there a r ...

AngularJS - The requested header field is not permitted by the preflight response of Access-Control-Allow-Headers

I'm currently utilizing the library https://github.com/doedje/jquery.soap/ to establish a connection between an Ionic application and an ASMX web service. However, I've encountered an issue: When attempting to connect to , I receive the f ...

What is the best method for sending a PHP variable to an AJAX request?

I am working with three files - my main PHP file, functions.php, and my JS file. My challenge is to pass a PHP variable to JavaScript. Below is a snippet from my MAIN PHP FILE: function ccss_show_tag_recipes() { //PHP code here } Next, here's t ...

Prevent the execution of a Javascript function if it is already in progress

I've developed a function that retrieves records from a third party, and this function runs every 10 seconds. However, as I debug through Firefox, I notice a long queue of ajax requests. I'm contemplating including a statement that can signal to ...

Reading through various files simultaneously using JavaScript: iterating over the same file in a for loop

I'm currently working with the following code: function handleFiles(e) { var filesInput = $('#files').prop('files'); var i, f; for (i = 0, f = filesInput[i]; i != filesInput.length; ++i) { var name ...

Content displayed in the center of a modal

Struggling to center the captcha when clicking the submit button. Check out the provided jsfiddle for more details. https://jsfiddle.net/rzant4kb/ <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class=" ...

Create compelling visual representations by utilizing Google charts to draw customized charts based on variable

I have been attempting to create a chart using Google Charts by passing some parameters to the function. I am trying to use these parameters to populate the data, but it doesn't seem to be working as expected. Is it possible to include parameters in t ...

What steps can be taken to rectify the issue with the table during the export to

I am using a library called DataTable to present my data in a table format. The DataTable has buttons on the header, one of which allows exporting the data into an Excel file. However, when I try to open the exported data in Excel, I encounter an error sta ...

Magento issue with unresponsive image map functionality

My website is built using the Ultimo theme in Magento, and I have implemented a script from to make the image map responsive. Here is the HTML code: <img name="sale" src="sale_1.jpg" width="1180" height="200" id="sale" usemap="#m_sale" alt="" />< ...

An easy way to add an array to another array within a Redux reducer

Imagine having this condition in the reducer: case POST_LOAD_SUCCESS: return { ...state, posts: [...payload] } The payload for this action is an array [{post_1},{post_2},{post_3},{post4},{post5}]. When this acti ...

Implementing validation for a Textbox based on changes in another component's value in React.js

Is it possible to trigger validation of a Textbox based on the value change of another custom component that updates the state? Handlers: handleValueChange = (val, elementName) => { this.setState({ ...this.state, [elementName]: val ...

Guide to resetting a CSS animation with pure JavaScript

My flexbox contains 4 images, each triggering an animation on hover. However, the animation stops if I hover over the same image again. I have tried various JavaScript solutions found online to restart the animation, but none seem to be working for me. S ...

How can you merge one object with two different mongoose models?

Consider the scenario where I have two mongoose models set up: User Model Business Favorite Model Currently, I'm successfully retrieving the combined result if a user has any favorite businesses. However, I suspect that my current implementation mi ...

Warning: Attempting to modify a property that is not defined - 'action'

My code includes a datatable and an alert that pops out. The datatable functions properly with or without the alert, but the alert does not work well when combined with the datatable. Could this be causing a conflict in the code? An error message indicates ...

Obtain information from express middleware

I am currently working on a project using node.js. As part of my application, I have developed a middleware function that is triggered whenever a GET request is made. This occurs when someone visits the home page, profile page, or any other page within my ...

What is the best way to delete an element from a list in a Meteor data source?

Currently, I am working through a tutorial on Meteor and attempting to delete an item. The code snippet I have been using is: lists.remove({Category:"Fraggles"}) Unfortunately, this method no longer works in the latest version of Meteor and I am encounte ...

Changes made in Vuex will not be saved until they are manually committed using developer tools

One of the modules in my Vuex store is called login.js and it looks like this: import axios from "axios"; import router from "@/router"; axios.defaults.baseURL = process.env.VUE_APP_API_ENDPOINT; const state = { access_token: localStorage.getItem("acc ...

Plotting a course on Google Maps using various sets of latitude and longitude coordinates retrieved from a database

I've successfully created PHP and JavaScript code that retrieves longitude and latitude information from a database and places markers on a Google map based on these values. Now, I want to connect these markers and plot a route on the map. How can I ...

Using multiple unique markers on Google Maps

Currently trying to grasp the concept of creating a customized Google map. I have limited experience with javascript but I'm eager to learn. I found some code online that helped me understand how to add locations, markers, and infowindows, but now I ...

Bring your Electronic Catalogue to life with the addition of dynamic HTML content showcasing the latest

I am creating a digital magazine but I am facing the challenge of having to deal with 200 pages in jpg format. To streamline the process, I am looking for a way to use a combination of JavaScript and PHP to avoid manually coding for each of the 200 pages. ...