0

Can you make a page automatically click into an iframe?

I have an iframe showing a game of tetris from another html document and I want it to automatically let me control the pieces when I click a button to make it appear on the page.

Here is some code:

<iframe id="tetrislauncher" src="src/tetris/index.html"></iframe>

<style>#tetrislauncher{display:none;}</style>

<button onclick="$('#tetrislauncher').fadeIn()">Play Tetris</button

I want the button to also do something like

$('#tetrislauncher').click()

but that doesn't work, I'm assuming because you don't click an iframe to call a function like you would in a button.

The point of this is that the game starts automatically, and pauses when you hide the game, but I want to click to make it appear, and be able to access the arrow keys to move the pieces without having to click the iframe beforehand. I'm having the same issue with another iframe playing space invaders, which is made using the same code (basically).

Anybody have any idea how this can be done?

Fin
  • 353
  • 3
  • 5
  • 16
  • Isn't this browser-jacking of some sort? I'm pretty sure Google Chrome and Mozilla (not sure about the "smarties" who made IE) would crack down on this sort of thing to help keep their consumers' privacy safe. –  Mar 04 '17 at 04:02
  • Do you have editing access to the other website? If so, are both pages on the same domain? – zer00ne Mar 04 '17 at 05:04

2 Answers2

0

You likely need to focus on the element.

$('#tetrislauncher')[0].contentWindow.focus()

Setting focus to iframe contents

Community
  • 1
  • 1
Jake Holzinger
  • 5,783
  • 2
  • 19
  • 33
0

Finley,

  • The button need an id tag as the iframe
  • Your button are inside the iframe?

Using jQuery:

$("#tetrislauncher").contents("#btnID").click();
$("#tetrislauncher").contents("#btnID").click(functionIfYouWant);

Pure javascript:

var iframeObj = document.getElementById("tetrislauncher");
btnObj = iframeObj.contentWindow.document.getElementById("btnID")
btnObj.addEventListener("click", functionIfYouWant, false);

HTML suggestion:

<button id="btnID" onclick="$('#tetrislauncher').fadeIn()">Play Tetris</button>
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58