Calling a js function embedded in html in your iframe?

  • I need help understanding how execute javascript from the browser or javascript object can, say, call a javascript function within the embedded html of an iframe... How does this work?

  • 	ACTION > Browser > Execute JavaScript > "yourfunction();"
    

    If you are calling a JS string as a C3 value.

    	Browser.ExecJS("yourvar;")
    
    	Browser.ExecJS("yourvar='newvalue';")
    

    If it is in an iframe or object one directory deep use:

    	ACTION > Browser > Execute JavaScript > window.parent.yourfunction();
    
  • That doesn't appear to work. If I load this into the iframe:

    <!DOCTYPE html>
    <html>
    	<head>
    		<script>
    			function myFunction()
    			{
    				alert("hello");
    			}
    		
    		</script>
    	</head>
    	<body>
    		
    	</body>
    </html>
    

    Then with the browser object execute javascript => "myFunction();"

    It doesn't do anything.

    But wait!

    I think I figured it out!

    "document.getElementById(""myFrame"").contentWindow.myFunction();" seems to work!

    (assuming we set the iFrame's id property to "myFrame")

    I don't know if this is safe in all environments though but it seems to be working for me... Yay.

  • The example scripts I posted previously are all intended to be used in "Actions" within C3 or C2Runtime not from your html. If the "iframe" or "Object" is in a different directory that's where the "window.parent" plays in. As well your Javascript should be called from a ".js" file rather than inline scripts.

    Also, I found that Construct games work better in an "embedded object" and not an "iFrame". However of course use whatever works best for your project. Just something to try.

     <object>
     <embed src="index.html" width="100%" height="100%"></embed>
     </object>
    
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yes, actions reference other actions in the way you described. If your browser or js object execute js action is trying to talk to a function embedded in an iframe then you have to qualify the iframe to get into it.

    For my project, I'm using the ajax object to get the contents of an html file in my files and then running it as html in the iframe object. In this way I'm getting an ESRI map to show up in Construct3 using ESRI's javascript api, and I can access it by calling JS functions in that frame.

    However it looks like iFrame is not supported in Construct 3 export to Android so I'd like to do it with something else that is.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)