PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 14, 2022

[FIXED] Why does iFrame in React not respond to click events

 April 14, 2022     iframe, reactjs     No comments   

Issue

I have a fairly simple react component which contains an iFrame. This component will load files from the server but there is no response to click events with either anchor elements or buttons. There isn't any scripting on the rendered page (with the anchor - I added a simple script for the button clicks).

Here is my code for the iFrame component, the calling component and the page being rendered with the links

import React, { Component } from 'react';
/*
Required parameters
    url:  the address of the page to show in iFrame
    title:  the title of the item for the iFrame title
*/

class IFrameComponent extends Component {

  state = { contentHeight: 500 };

  handleResize = () => {
    const top=this.container.offsetTop;
    this.setState({contentHeight: (this.getWindowDimensions().height-(top+55))})
  };

  getWindowDimensions = () => {
    const { innerWidth: width, innerHeight: height } = window;
    
    return {
      width,
      height
    };
  }

  onLoad = () => {
    this.container.contentWindow.addEventListener('resize', this.handleResize);
    window.addEventListener('resize', this.handleResize)
    this.handleResize();
  }

  componentWillUnmount() {
    this.container.contentWindow.removeEventListener('resize', this.handleResize);
  }

  render() {
    const { contentHeight } = this.state;
    return (
      <iframe
        frameBorder="0"
        onLoad={this.onLoad}
        ref={(container) => { this.container = container; }}
        scrolling="no"
        src={this.props.url}
        style={{ width: '90%', margin:'5% auto', height: `${contentHeight}px`, zIndex:20000 }}
        height={contentHeight}
        title="Some Content"
      />
    );
  }
}

export default IFrameComponent;
import React from 'react'
import IFrameComponent from '../Elements/IFrameComponent';

const WorkplanFiles = ({ site }) => {
    let path=`${process.env.REACT_APP_HOST}/workplans/index.html`
    return <IFrameComponent url={path} title='' />
}

export default WorkplanFiles;
<html>

<head>
    <link href="/workplans/StyleSheet.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        handleClick=(url)=>{
            alert(url)
            location.href=url;
        }
    </script>
</head>

<body>
    <div style="width:100%; text-align:center;">
        <p class="heading">MCA Workplans</p><br />
    </div>

    <div style="width:100%; text-align:center; z-index:5000">
        <table cellpadding="5" style="position:absolute">
            <tr>
                <td><button onClick='handleClick("/workplans/ASL_Final.htm")'>
                    ASL Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/ASL_Workplan.pdf#ASL=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td><button onClick='handleClick("/workplans/narratives/ASL_Narrative.pdf")'>
                    Narrative</button></td>
            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/Cable-ISP Workplan.htm")'>
                    Cable/ISP Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/Cable_Workplan.pdf#Cable=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td><button onClick='handleClick("/workplans/narratives/Cable_Narrative.pdf")'>
                    Narrative</button></td>
            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/Rogers Global v.1.4.htm")'>
                    Rogers Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/Rogers_Flowchart.pdf#Rogers=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td><button onClick='handleClick("/workplans/narratives/Rogers_Narrative.pdf")'>
                    Narrative</button></td>
            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/TMI Global.htm")'>
                    TMI Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/TMI_Workplan.pdf#TMI=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td><button onClick='handleClick("/workplans/narratives/TMI_Narrative.pdf")'>
                    Narrative</button></td>
            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/Bell Global.htm")'>
                    Bell Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/Bell_Workplan.pdf#BELL=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td><button onClick='handleClick("/workplans/narratives/Bell_Narrative.pdf")'>
                    Narrative</button></td>
            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/Bell 2nds V.2.htm")'>
                    Bell 2nd Placements</button></td>
                <td>
                    <button onClick='handleClick("/workplans/Bell 2nds.pdf#BELL=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td></td>
            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/TCI Workplan.htm")'>
                    TCI Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/TCI Workplan.pdf#TCI=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td><button onClick='handleClick("/workplans/narratives/TCI_Narrative.pdf")'>
                    Narrative</button></td>
            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/CIT Workplan V.1.htm")'>
                    CIT Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/CIT Workplan.pdf#CIT=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td><button onClick='handleClick("/workplans/narratives/CIT_Narrative.pdf")'>
                    Narrative</button></td>
            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/Basic Work plan (Preferred).htm")'>
                    Preferred Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/Basic Work plan (Starchoice).pdf#Preferred=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td></td>

            </tr>
            <tr>
                <td><button onClick='handleClick("/workplans/Basic Work plan (Starchoice).htm")'>
                    Starchoice Work Plan</button></td>
                <td>
                    <button onClick='handleClick("/workplans/Basic Work plan (Starchoice).pdf#Starchoice=Workplan")'>
                        <img src="/workplans/Adobe.jpg" class="pdfLogo">
                    </button>
                </td>
                <td></td>
            </tr>

        </table>
    </div>
</body>

</html>

Solution

Using the 'sendFile' router requisition function doesn't appear to set the content-type correctly. Although the page displays nothing functions. Using

res.contentType(mime.lookup(path)) //(mime-types npm package) to set the contentType header
res.send (readFileSync(path)) 

allows the document to function correctly.



Answered By - Geoff
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing