Redirect page and call javascript function

javascript, jquery

Solution

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.

Problem

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.

Original source

Related problems