On some occasions you may want to open a link in a new tab a GameMaker' HTML5 game.
As simple as it may look, this presents a bit of a problem:
-
url_open opens links in the same tab.
-
url_open_ext can open links in a new tab, but triggers popup blocker.
-
clickables can open links in a new tab, but have to be repositioned manually.
(particularly inconvenient if scaling-positioning the game for mobile browsers)
-
Adding an actual link (<a> element) into the game template allows to open links in a new tab, but requires basic understanding of JavaScript and HTML to hide/unhide dynamically.
url_open_ext is by far the most convenient of these, so let me explain why that does not work:
To prevent any page from being able to randomly open indefinitely large quantities of new tabs, the browser will automatically block attempts to open new tabs\windows unless they originate from user interaction (click event);
GameMaker handles events, and writes down new input states to later dispatch GML-level events at the right time and place (see event order). This means that your GML code inside a "Mouse Pressed" event does not count as originating from a user interaction (as it executes a few milliseconds later), and thus is not allowed to open new tabs (and do some other things).
However, with a bit of JS (and understanding of internal workings of GM), it is possible to accomplish the intended result, and this post is about that.
Continue reading →