0

I know the title sound similar with some other threads but it's a little bit different.I have few buttons like those:

<input type=button name=Ok onclick='window.location="www.bla.com/bla.php"'>

When the user press any of them,i want a javascript or jquery code to run, but after,i want to be send to www.bla.com/bla.php.

I have a jquery code for submitting the form:

$( document ).ready(function() {
   $(':button').click(function(){
    document.getElementById('contact').submit();    
    }); 

The jquery code works,but the buttons does not...

12
  • @Teemu sorry,i forgot about window.location Commented Jan 6, 2015 at 10:37
  • You mixed up the " and ' in your Html. Commented Jan 6, 2015 at 10:39
  • @Fuzzyma How is that? Both are valid in JS and HTML. Commented Jan 6, 2015 at 10:40
  • yes but it's recommended to stay with the standard which is " in Html Commented Jan 6, 2015 at 10:42
  • @PetruLebada I guess you've this same problem, submitting a form will prevent setting new window.location to work. Commented Jan 6, 2015 at 10:44

4 Answers 4

2

My solution is jQuery based.

The syntax here is incorrect:

<input type=button name=Ok onclick="www.bla.com/bla.php">

Modify it as:

<input type=button name=Ok data-href="www.bla.com/bla.php">

jQuery:

$( document ).ready(function() {
  $(':button').click(function(){
  var hrefCurr = $(this).attr('data-href');
  document.getElementById('contact').submit();
  window.location.href = hrefCurr;
});
Sign up to request clarification or add additional context in comments.

3 Comments

sorry i forgot about window.location .. check my question again please
This is dynamic done. Please read it carefully, it should work.
if form.submit succeeds your javascript will stop running, do you exepct form.submit to fail?
2

try this

<input class="btn" type=button name=Ok data-href="location">

$( document ).ready(function() {
   $('.btn').click(function(){

    // as you have used jQuery selector you can use it here like this 
    $('#contact').submit();   

    var location = $(this).attr("data-href");

    window.location.href  =  location;
    }); 
});

3 Comments

your answer might work,but i need it to be more particular,because i have a lot of buttons and each of them send you to different pages
you can specify location in data-href for each button at time of clicking button it will take url of that button and redirect to that url
you can specify location in the action attribute of the button.
2

No javascript will execute after the form is submitted. form submission changes window.location and that stops the javascritpt from the old window.location from executing.

but you can fake it.

  • use jquery forms to submit the form (if CORS will allow that)

  • or have the script at the form destination do a redirect to your desired location.

  • or submit to the desired location and have the script there send the form data to where it should go

1 Comment

To be exact, you can execute some JS, since there's a delay before a new page is loaded, but the change of the location.href is blocked immediately.
1

Something like this could do the job?

<input type='button' name='ok_btn' id='ok_btn' value='Ok'  data-href="www.bla.com/bla.php" />

$(document).ready(
    function() {
        $('#ok_btn).on('click',function(){
            // Do your stuff
            // Finally:
            window.location.replace($(this).attr('data-href'));
        });
    }
);

2 Comments

This will work, since the form is not submitted in the snippet. It's not what OP wants though.
Hm, so replace window.location... by $('#contact').attr('action',$(this).attr('data-href')); $('#contact').submit(); could do it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.