Detect google chrome on click and redirect if it's not

1.5k views Asked by At

I want a browser detection onClick on a button and if the result is NO. The page will redirect to the given url. The button is a submit type in a form. I can make it a browser check but when It alerts. The redirect is not go - still submit form.

HTML

<button type="submit" onClick="isChrome()";>submit</button>

Script

function isChrome() {
    $.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); 
    event.preventDefault();
    if(!$.browser.chrome){
        alert('Please use only Google Chrome!');
        window.location="http://www.google.com/chrome/";
        return false;
        stop();
    }
}

FIDDLE HERE : http://jsfiddle.net/nobuts/g89k68ge/

3

There are 3 answers

2
Ken Kwok On

if you don't want to submit the form, you can simply create a button instead. Why bother to cancel the submit function

0
Wilf On

I found out my solution! Thank you everyone for the help and clues. @Ken, your clue enlighten me :D

function isChrome(e) {
    $chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); 

    if(!$chrome){
        alert('Only Google Chrome!');
        //window.location="http://www.google.com/chrome/";
        $('#date_form').attr('action', 'http://www.google.com/chrome/');
        return false;
    }else{
        $('#date_form').attr('action', 'form_action_URL'        }
}
0
karanmhatre On

Here's using jQuery and the submit event trigger.

https://jsfiddle.net/karanmhatre/o8d40e7v/1/

HTML

<form method="get" action="http://google.com" class="form">
    <button type="submit">submit</button>
</form>

JS

$('.form').submit(function(event) {
        var condition = false;
        event.preventDefault();
        if (!condition) {
            alert('Please use only Google Chrome!');
            window.location = "http://www.google.com/chrome/";
            return false;
            stop();
        }
    });

I have abstracted the code to assume the condition to be false for testing purposes.