2

I have been working with a page that redirects a user to either an installed application or a webpage (the fallback). This is called when the page first loads via ClientScript.RegisterStartupScript; the Javascript portion looks something like this:

<script type='text/javascript'>var a = window.location.search; setTimeout(function(){ window.location.pathname = '/Fallback.aspx'}, 500); window.location='myapp://open' + a;</script>

So far, this snippet always functions as expected in Google Chrome, redirecting the user to the Fallback page whenever the 'myapp://open' fails to open correctly within the given amount of time. In Internet Explorer, it only works when the timeout value is set to 100 or lower. My problem at this moment is Firefox, which fails to redirect correctly no matter what the timeout value is set to. (For Firefox, I have tried values of as little as 25 and as high as 2000.)

Does anyone know what the Firefox browser might do differently that would prevent it from redirecting, and if so, is there any known workaround for it?

Thank you very much in advance for your time and advice.

UPDATE: The exact error page I am getting from Firefox is titled "The address wasn't understood", with the description similar to the following: "Firefox doesn't know how to open this address, because the protocol (myapp) isn't associated with any program."

UPDATE: To test this, you can replace '/Fallback.aspx' in the code with 'www.google.com'. If this is tried in IE or Chrome, the browser will fail to open myapp://open and should redirect you to Google instead; this is the intended functionality since the application is not installed. However, in Firefox you will likely be left at the error page telling you the protocol is not recognized; it will fail to redirect to the fallback. I hope this helps, and I apologize for the original wording of my question.

user2912928
  • 170
  • 3
  • 15
  • I just tried your script in Firefox, replacing `myapp://open` with `http://google.com` and it redirected just fine. That being said, what error, if any, is Firefox reporting? Is it finding `myapp`, or erroring out when attempting to call that protocol? – James Lai Dec 20 '13 at 16:13
  • I have updated my question to include error information. The functionality I expect is that the myapp://open portion will fail, and that it will redirect to the web page at the Fallback.aspx path. It fails to open the myapp://open, but it never redirects to the fallback page. – user2912928 Dec 20 '13 at 16:18
  • Firefox isn't aware of the application protocol you're attempting to use. Replace `myapp://open` with `http://google.com` and you'll notice your code works. – James Lai Dec 20 '13 at 16:20
  • To clarify for other readers: the "error" here is actually an *error page*, not a script-level error. Firefox redirects *immediately*, but it redirects to a page with a big error message on it. (At least, that's what I see on my FF install.) – apsillers Dec 20 '13 at 16:21
  • My apologies, I may have worded my question a little strangely. To clarify, opening the app is supposed to fail because the application is not installed, so the protocol will not be recognized. My problem is that the browser is then not redirecting to the Fallback page. In IE or Chrome, the page will first fail to find the protocol / open the application, and then it will redirect to Fallback.aspx. In Firefox, the browser just stays on that broken app page and never redirects. I need Firefox to redirect to Fallback when the myapp protocol is not understood. – user2912928 Dec 20 '13 at 16:23
  • IIRC, the question appears to be that the user has 2 links, a primary and a secondary. The browser should redirect the user an app on their phone first, and if it fails to do that it should fall back to the second link and redirect them to whatever the web address is. Testing the code, since none of us have the app available to us and many are on desktops, it SHOULD always fail and open the second link. However, it appears that while IE and Chrome are functioning properly, Firefox is failing to redirect properly. Unfortunately I don't know the answer, but hope this helps clarify for others. – C Smith Dec 20 '13 at 16:33
  • 2
    If the top answer on [How to check if a custom protocol supported](http://stackoverflow.com/questions/2872090/how-to-check-if-a-custom-protocol-supported) still works in Firefox, that might do it for you. – apsillers Dec 20 '13 at 16:55
  • Thank you, apsillers; that does still work for Firefox. =) I will look into a way of integrating this with my current code so that Firefox will be treated differently from IE and Chrome. – user2912928 Dec 20 '13 at 17:08

1 Answers1

0

I have found a few different ways to get around this for anyone who may stumble across this question with the same issue. =) The first is by using redirect code in code behind, singling out the Firefox browser which needs to be handled differently:

string userAgent = Request.ServerVariables["HTTP_USER_AGENT"];
                if (userAgent.Contains("Firefox") && !userAgent.Contains("Seamonkey"))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "checkForApp", "<script type='text/javascript'>var a = window.location.search; try { window.location.href='myapp://open' + a; } catch(e) { window.location.pathname = './Fallback';  }</script>");    //Firefox Only
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "checkForApp", "<script type='text/javascript'>var a = window.location.search; setTimeout(function(){ window.location.pathname = './Fallback.aspx'; }, 100); window.location.href='myapp://open' + a;</script>");    // IE & Chrome
                }

Although it worked, I do not like this method because it examines the user agent. After this someone suggested to me that I should put an iframe on my fallback page to open the app and just direct everyone to the fallback page instead (which still opens, but if the app is installed, it should open too.) This works in most browsers but not Internet Explorer:

<iframe name="open_app" id="open_app" src="myapp://open" style="height: 1px; width: 1px; visibility:hidden;" ></iframe>

The method I finally decided to go with was an object tag on the fallback page. So far, this seems to work in most major browsers and tested successfully using Chrome, Firefox, Safari, and Internet Explorer. Using this, the fallback page is still opened in the user's browser, but the app will also be opened if it is installed.

<object data="myapp://open<%= Request.Url.Query %>"/>
user2912928
  • 170
  • 3
  • 15