Submiting a form from javascript (using jQuery) opens in new tab in Opera 12.16

I have a huge and annoying problem.

I'm using jQuery (version 1.7.1, it's not an option to update jQuery or the browser).

I added some Javascript to handle <ctrl>+s as a submit, instead of a save.

It works perfectly well in every browser, except Opera.

In Opera, it submits, but to a new page.

If anyone has found a way to fix this, can you please provide some help? I would appreciate a lot!

This is quite an annoying bug and I use that feature a lot in the backoffices I'm developing.

Here is an example of the Javascript code used:

$(window).keydown(function(e){
    if(e.ctrlKey&&e.keyCode==83)
    {
        e.preventDefault();//disable page saving
        var a=$(':focus');//gets the focused element
        if(!a.length){a=$('form:eq(0)');}else{a=a.parents('form');}//if no element is focused, gets the 1st form
        a.find("input[type=button].submit, button[type=submit], input[type=submit], form a.submit").click();//triggers the click in the button
        if(a.find('input[type=submit]'))a.submit();//'silly' line added as a desperate attempt, ignore it
    }
});

The HTML code is a simple form like this:

<form action="#" method="POST" [target="_self"]>
    <input type="text" name="fld">
    <input type="submit" name="sub" value="Submit">
</form>

Using a <button type="submit"> submits to the same page AND to a new tab.

Answers


I found a solution!

This is a really UGLY hack.

This is the new Javascript:

$(window).keydown(function(e){
    if(e.ctrlKey&&e.keyCode==83)
    {
        e.preventDefault();
        var a=$(':focus');
        if(!a.length){a=$('form:eq(0)');}else{a=a.parents('form');}
        a.find("input[type=button].submit, form a.submit").click();
        if(window.opera&&a.find('input[type=submit],button[type=submit]').length)a.submit();
        else a.find("input[type=submit],button[type=submit]").click();
    }
});

And the HTML must change to this:

<form action="#" method="POST" [target="_self"]>
    <input type="text" name="fld">
    <input type="hidden" name="sub" value="1"><!-- to validate if the form was submitted on opera -->
    <input type="submit" name="sub" value="Submit">
</form>

Since the form submission works perfectly on the other browsers, we can leave the default behavior for them, changing only the way that Opera works.

As I said: it's an UGLY hack, but works.

Thank you @Bergi for your idea. I just had to adapt it.


Need Your Help

Foursquare Explore API not providing venue tips on certain accounts

json api foursquare

When using the Foursquare Developer API to perform a venue search, I receive different results depending on the account being used.