Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript error causes page reload?

I have been getting some weird page refresh in deployment and I can't seem to find the cause of it or replicate it in development. Is there a way for a javascript error or exception to cause a page reload? Or some tips to help me narrow down the cause?

It happens when deployed in the field and I can't replicate it while testing locally. I know it's happening as I'm logging exceptions using ELMAH. User is using Firefox.

UrlReferrer: example.com/products/edit/100

Url of GET: example.com/products/edit/undefined

And I don't see any javascript code in the form of window.location = '/products/edit/' + id, (where id might be undefined) that could be called from that page.

But there are calls like the above elsewhere. I hope that made sense. :)

like image 592
sean Avatar asked Jan 23 '26 02:01

sean


2 Answers

No, there is no Javascript error that by itself would cause a page to reload. There is no reason for the browser to reload the page if an error occurs, the only thing that it could hope to achieve by that is to cause the same error to happen again.

So, the only reason that a Javscript error could cause a reload is if the script is preventing a reload when it's working properly, or if it normally causes a reload in some specific situation and fails to limit it to that situation.

Example: If you have code that prevents a click from causing a postback:

<a href="page.html" onclick="return false;">

If there was an error in the script it would no longer prevent the reload.

like image 156
Guffa Avatar answered Jan 25 '26 16:01

Guffa


The question has already been answered, but I wanted to add a suggestion on debugging this behaviour.

I am using Firefox / Firebug for a lot of JS development, and sometimes I will see an exception raised in Firebug right before the page submits and there's nothing I can do about it. If I have 100s of lines of JS in response to an event I don't want to step through it all looking for the 1 line that contains an error and causes a reload.

Once I can replicate the error I use Fiddler to set a breakpoint on a url (e.g. bpu localhost which will halt any requests headed for localhost. Even though the browser has submitted it still provides access to Firebug so I can inspect the error while Fiddler holds the request.

Hope this helps someone

like image 22
tomfumb Avatar answered Jan 25 '26 17:01

tomfumb



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!