Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Change page1.htm to page2.htm in iFrame2 in table row 1 cell 0

ggunzelman

New Coder
I have a simple information page that I display in an iFrame. The web page consists of a single table with 2 rows. The top row has a logo and some buttons. The 2nd row displays a .htm page in an iFrame. We'll call this page2024.htm. In the top row there is a Select construct to allow the user to choose page2023.htm. These are historical information pages. I can see the Select working as I watch the Chrome Console log but I cannot get the page to change properly. Instead of displaying the page in the same cell, it overwrites the entire web page with page2023.htm. Below is the code in question. I first set the cell to Page2024.htm and then a script executes on the Select change to change the page to the value in Select. I cannot figure out what is wrong. I am new to Javascript. The site was done in Dreamweaver and the two pages to display are .htm saves of Excel files.

HTML:
<tr data-id="xlsxDisplay" bgcolor="#FFFFFF">
<td  colspan="2">
<iframe name="iFrame2" id="iFrame2" width="1980" height="925" frameborder="0" scrolling="yes" src="Page2024.htm"></iframe>
&nbsp;
<a href="Page2024.xlsx">Download Excel File</a>

<script type="text/javascript">
  $('#navYear').on('change',function(){
    var url = $(this).val();
    console.log(url)
    document.getElementById("xlsxDisplay").innerHTML= document.write('<iframe id="iFrame2" scrolling="yes" height="925" width ="1980" frameborder="0" src='+url+'><\/iframe>');
  })
</script>
 
Last edited by a moderator:
From the code you have provided, there's no element with the xlsxDisplay id. If you meant to select the data-id="xlsxDisplay", then use the following syntax:
JavaScript:
document.querySelector("[data-id=xlsxDisplay]")

// or since you're using jQuery anyway, just use this:
$("[data-id=xlsxDisplay]")

The document.write() method writes a text string to an open document stream. In your case, since there is no open document, the entire page will be replaced with what is inside the write() method.
The use of this method doesn't return any value and cannot be assigned to an element's innerHTML;

For normal JavaScript you would have to create the iframe element using the document.createElement() method, but since you're using jQuery, you can do the simpler way, by using the html() method:
JavaScript:
$(selector).html("<p>this text will be input in the inner html of the selected element</p>");

There is another way of changing the content of the iframe, rather than replacing the entire frame. This can be done with setAttribute() method on the iframe element (attr() for jQuery):
JavaScript:
// plain js
document.querySelector(selector).setAttribute("src", new_source_url);

// jQuery
$(selector).attr("src", new_source_url);


Here's the full jQuery code using the attr() method:
JavaScript:
$("#navYear").on("change", function() {
  $("#iframe2").attr("src", $(this).val());
});
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom