3

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. :)

Taryn East
  • 27,486
  • 9
  • 86
  • 108
sean
  • 11,164
  • 8
  • 48
  • 56
  • 2
    Which browser are you testing with? Is it occurring in all browsers? Do you have any refreshing/reloading code in your JS anywhere? Have you tried using Firebug/IE dev tools/Chrome dev tools to step through the code? – Andy E May 09 '10 at 14:09
  • 5
    @Andy - Step away from the coffee! If you just pop in and read that, it sounds like a co-worker on a caffeine craze – Nick Craver May 09 '10 at 14:12
  • @Nick - Then let's put it simple; OP, can you provide us with an example, like some code or a link? – Sune Rasmussen May 09 '10 at 14:17
  • Added some more details to question. Hmmm. I can't really post the log. – sean May 09 '10 at 14:21
  • @Nick: yeah maybe I should have slowed it down a bit ;-) – Andy E May 09 '10 at 15:02
  • This question about button reloads was relevant (for me): http://stackoverflow.com/questions/1878264/how-do-i-make-an-html-button-not-reload-the-page – JJ Rohrer Jul 29 '14 at 20:29

2 Answers2

7

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

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

tomfumb
  • 3,669
  • 3
  • 34
  • 50
  • 1
    @redolent it's an extremely useful (and free) .net tool for monitoring, trapping, and altering HTTP requests and responses on your machine. It's web development's missing piece and its place is between your browser debugger (Firebug, Chrome tools) and server-side debugger (VS, Eclipse, x-debug). It can teach you a lot about HTTP and also help you test your application without altering code or writing a test harness. http://www.fiddler2.com/fiddler2/ – tomfumb Mar 05 '13 at 01:46