Preventing unwanted redirects from browser to app store

不羁岁月 提交于 2019-12-11 10:06:25

问题


I've been working on a redirect page that sits between an ad and the app store. The ad exists as a static URL that directs to the redirect page. The redirect page sends an ajax request to a third party, sets a cookie, then redirects to the AppStore. All well and good and not uncommon.

The redirect page cannot close itself so it remains as a tab in Safari. The issue I'm having is that when the user returns to Safari if the page is been purged from the cache, Safari will reload it triggering the redirect. I do not want the users getting thrown into the AppStore unexpectedly.

One solution would be to check for the presence of a cookie and not redirect if it's presence, but this leaves the edge case of the user clicking on another banner ad and not getting the appropriate redirect. I've tried appending an anchor to the URL which prevents user initiated refreshes, but the auto-refresh mechanism of Safari does not respect the programmatically added hash.

If I could use a dynamic source to generate the URL that directs the user to the page I could generate a timestamp, but right now the origin URL is static. Does anyone have a solution for this using client side code? Or is this really only solvable using a server-side solution?


回答1:


I ended up choosing to do a two stage redirect. User clicks a banner and gets directed to:

http://myserver.example.com?someKey=someValue

I have a function that does this:

// Do I have a visited param?
if ($.url(window.location.href).param('visited') === '1') {
    // Do I have a visited cookie?
    if (helper.retrieveCookie('VISITED') == undefined) {
        console.log('Setting visited cookie');
        helper.storeCookie('VISITED', '1');
        return 1; // Redirect to AppStore.
    }
    // Have param and cookie
    console.log('VISITED cookie set');
    console.log('Refreshed');
    return 2; // Don't redirect.
} else {
    // No param
    helper.removeCookie('VISITED');
    return 0; // Redirect to self with &visited=1
}

So basically we will pass through the code twice meaningfully and N times subsequently due to refreshes. During the first pass we do our AJAX request, then redirect to ourself with an added param. During the second pass we set a cookie and redirect to the AppStore. Any subsequent load of the page will have both a cookie and a param and won't redirect. New banner clicks will not have the param so they will perform normally.

This isn't the most beautiful solution since we have to reload our redirect page, but since its contents should be cached, the hit should be minimal.




回答2:


How about adding a javascript on the landing page?

if (history.length < 2) 
  history.back();
else
  location.href = '/thankyoufordownloading.htm';

I haven't tested it myself but it could work.



来源:https://stackoverflow.com/questions/12059464/preventing-unwanted-redirects-from-browser-to-app-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!