Hey Friends,
I have a 3D unity file embedded the HTML of a site. I would like to use Application.OpenURL to launch a new browser window to a link and another to launch a new link in an iFrame on the same page.
Anyone have some sample code to make these two functions work?
Hey Friends,
Ok. I have an object in a Unity web player that can be clicked and it then updates an iFrame.
First I load up a little JS in to the header of the HTML page that the player is embedded in too that reads like this:
function loadIframe(theURL) {
document.getElementById(“news”).src = theURL;
}
With the iFrame name id being “news”.
Then I attached the following script to a clickable object in the player:
function OnMouseUp () {
Application.ExternalCall (“loadIframe”, “‘newsFrame2.html’”);
}
With “newsFrame2.html” being the HTML page to load in to the frame.
Works like a charm.
BUT… I still do not have a good way to load a brand new browser window from a link in the player. Anyone have sample code that might work with this?
I’d do exactly what you’re doing with the iFrame (javascript function in the HTML), but replace the loadIframe function with an open window one (can’t remember exactly what the name/perimeters of an open window function are, but you could probably look them up)
Man, you’re ever so close with your own line of thinking!
Why not like this:
function OnMouseUp () {
var tJSString = "window.open('url','window name','attributes...')";
Application.ExternalEval(tJSString);
}
Just use some simple browser JavaScript to spawn a new window showing the desired URL instead of calling a function that sets a page element’s src property.
remember the popup-handling of the different browsers, wasted some time with this myself…
Very good advice, otherwise you end in the land of “skip it, popup blocking is there to prevent people from spawning annoying windows and it does not care if the window is good or bad”
If you want to show something in addition to what you have you have 3 options:
- DIV
- IFRAME
- replace your own url with the new destination
Whats no option is window.open unless you preinform the user to add you to the exceptions / accept the popup request cause anything aside of IE 6 and other stone age browsers, which have no plugin installed for it, will block it under normal circumstances
Personally I recommend solution 2, for the reason that IFRAMES commonly also work rather well at overlaying unity content on more than chrome and safari on OSX 
Window.open is a great quick fix but it doesn’t work by todays standards. it will just be blocked my most. just tried it.
I have an iPhone game I have just ported to the web with links to the AppStore and another site. Unfortunately, the only real solution I can use is a direct link which would leave the game. I cannot use iFrames or anything the relies on external script (unless the script is created in the app) as I may not be in control of the the html that delivers the app.