Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect page and call javascript function

I need to redirect to another page onClick of submit and call a function making an ajax call. This is what I have :

$('#submitButton').click(function() {

window.location.href = "otherPage";
displayData();
});

Also, Another way I tried is to set the form fields on otherPage by using

var elem = window.document.getElementById(field);
elem.value = "new-value";

so that I could call the eventhandler of a submit button present on otherPage, but it doesn't work. Please let me know where I am wrong.

like image 634
SKaul Avatar asked Sep 05 '26 00:09

SKaul


1 Answers

I'm not sure if there is a neat way to achieve this, but you can add a hash in your url you redirect to, then just simply check if the hash exists, execute function and remove hash.

Here are some handy URLs:

  • Location.Hash - information about this function and how to use it.
  • Removing hash from url without a page refresh - This was a bit of an issue, as window.location.href = ''; removes everything after the hash.
like image 153
Arko Elsenaar Avatar answered Sep 07 '26 13:09

Arko Elsenaar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!