Search code examples
urlhref

Redirect a webbrowser if chrome 18 is not found


I created this script to hide a page only if chrome 18 has not been found, how can I make sure to do the redirect of the url to a external page if chrome 18 has not been found instead of hiding only the page ? I want to do the redirect to this website http://search.aol.com/aol/webhome

<div id="hiddenContent" style="display: none;">
My hidden content.
</div>

<script>
function GetChromeVersion() {     
    var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
    return raw ? parseInt(raw[2], 10) : false;
}
if (GetChromeVersion() == 18)
    document.getElementById("hiddenContent").style.display = "";
</script>


Solution

  • It's possible, e.g. by using:

    window.location.href='http://search.aol.com/aol/webhome';
    

    Inside your function. BUT I wouldn't recommend you this. You should never trust the client as JavaScript can easily be changed and someone could access your site even despite the JavaScript redirect if they wanted to and made a minimal effort to modify page sources. Javascript is handled on the client-side, while php is completely server-side. In this case I'd use php instead of JavaScript. Try to check it with php and if it's not chrome 18, use: header('Location: http://search.aol.com/aol/webhome');

    Edit:

    <?php function is_chrome() { return(eregi("chrome/18", $_SERVER['HTTP_USER_AGENT'])); }   if(is_chrome()) { header('Location: http://www.search.aol.com/aol/webhome'); } ?> Place it on top of the file, should work, but haven't tested it yet, if not, just change the string in eregi('chrome/18') to what you need (it's a regular expression)