Вопрос

I have 'black-box' type ASP framework, let's say I cannot modify it. But I can add HTML modules to it. I've made HTML module using AJAX for user logon and fetching data. The problem is I need to intercept submit event of the logon form. Then it has to call web service, return token and store it in a cookie before the page reloads.

First I need to stop the page from reloading. So I need to unbind events which cause the reload. But how? I don't have access to code which bound the events. It's not mine.

I was able to attach my own event handlers to submit button, login and password fields. They do their job - they start the AJAX request which should perform my logon procedure. Well, at least they try, because almost always my AJAX call is killed before it can finish, the page reloads, and my module has no token it should have by then. 1 in about 20 calls - it succeeds, so it looks like possible to do.

If there was a way to prevent page from reloading until the AJAX callback completes, it would probably be enough for it to work.

Is there absolutely no way of killing events which I din't bind in my code?

Это было полезно?

Решение

Well, I've found the way to achieve the goal without actually unbinding any events. I needed to replace the form action attribute with '#', the same with special submit link href ('#'), and... DONE! After my AJAX call completes (or fails), it restores original hrefs and submits the naughty form.

So the question I asked was wrong - my fault. I needed to prevent a form from being submited and this is way easier than removing alien events!

TL;DR - kill hrefs first, then restore them :)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top