- Home /
Open a link without popup-blocker
Hi. I want to create a button which opens a link to a new webpage. Currently I use the following inside of a OnMouseDown
method:
Application.ExternalEval("window.open('" + url + "','_blank')");
However this gets rightfully blocked by any popup blocker. I've done some research and found out, that the blocker only allows this when the funktion was called by a user event like the onclick event.
Outside of unity you could use the events like this (in a form class):
private Button someButton;
private EventHandler buttonEvent;
//inside of the constructor:
buttonEvent = new EventHandler(buttonFunktion);
someButton = new Button();
someButton.Click += newFormEvent;
private void buttonFunktion(object o, eventArgs e){}
My problem is that I have no idea how I could access the events which get fired when I press a mouse button over a collider.
Even if you were to somehow fake an onclick user event to open a link (which I tried but was still unable to avoid the popup blocker), you will not be able to run custom javascript from a site like kongregate. There isn't anything from the unity side that will allow you to bypass pop-up blockers (and probably with good reason), other than just redirecting from the current page with Application.OpenURL. It may be time to rethink how you will handle your credits/sponsors. hard coded credits? links in description?
Also, what is the major concern with these links being caught by a blocker? The sites will still be accessible and it doesn't sound like missing the blocked links would be detrimental to the gameplay (i.e. needing a code from an external page to continue gameplay).
I don't really want to bypass the popup-blocker. I simply want to make my credits links to work immediately. One reason is that I want to save the players the trouble of turning of the blocker. The other is the courtacy towards those I want to give credit to. They might loose some clicks because not everybody will turn of their blocker to check out some site.
Since it works with flash, I was wondering if this is possible with unity too.
Answer by rutter · Jan 29, 2014 at 01:13 AM
If your WebPlayer knows which page it's going to be shown on, you can call JavaScript that's on that page.
The Unity manual offers a pretty simple example, which I'll quote below.
Example page contains the following JS:
<script type="text/javascript" language="javascript">
<!--
function SayHello( arg )
{
// show the message
alert( arg );
}
-->
</script>
Example WebPlayer app calls the following:
Application.ExternalCall( "SayHello", "The game says hello!" );
Thanks for your reply.
Unfortunately the page does not know anything about what I'm going to link to.
I'm asking for the case of a game on a commercial games site like kongragate. The links I want to provide are credits links and (hopefully) sponsor links. So there is no way I can modify the page to set up custom js functions.
Besides: Since this would be pure js I'm pretty sure, that this would be cought by a popup-blocker too.
I haven't tried it, and it seems a little shady, but you might be able to use Application.ExternalEval to inject extra script tags into the page.
Let's overlook the fact that I'd feel pretty uncomfortable messing with other peoples web-sites.
This would still not work! I built a small test project which calls a function with window.open ...
on its site into my dropbox. Result was, that the popup-blocker caught this.
The blocker will catch everything that was not fired by a user events. Not matter if it is from the webplayer or the site itself.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Open a link 1 Answer
Input versus Event 1 Answer
How to fix this for c#? 1 Answer
How do I access a static event in another function? 1 Answer