Link to home
Create AccountLog in
Avatar of rascal
rascalFlag for United States of America

asked on

Firefox reloads page on its own when image src=""

Hi Experts,

Has anyone ever encountered a problem with FireFox where the browser automatically and transparently reloads the page when the page contains a missing image?

I've got a page that emails the administrator that the page has been entered, and the email goes out twice.

I used to see this on FireFox when an image src was not found such as <img src="#">

It was just a curiosity before, but now it's a bug I have to deal with. Is there a fix for keeping FireFox from reloading the page? Normally I would just fix the offending src="#" but on this page there is no src="#".

This page contains a tinymce w/imagemanager plugin and I believe what is happening is that the code that tinymce automatically inserts into the page on its initialization is somehow including a broken image link, thus triggering the FireFox reload.

I ran FireFox's "show broken images" report, but it only reports that there is a broken image on the page, it doesn't report any details about it.

Has anyone else encoutered this and is there a workaround?
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

A missing image shouldn't force a re-load. I would guess that something else is interfering. Try disabling javascript and see if the problem goes away. If it does, hunt down the script and fix it.
Are you using any plugins like YSlow - it could be these doing the rerequest, although that was a bug in older versions of FF
Check for duplicate meta tags.
This may help if it is still the img tag
http://www.planbox.com/blog/development/coding/browser-send-same-request-twice-or-multiple-times.html
If you can give a link, it may help, also check HTTPFox to see if you can identify the culprit
Avatar of rascal

ASKER

It's definitely a bad src= that causes it. You can repro this by creating a page that sends you an email at the top of the page, and has nothing else in the page but an image src="#"

Then view the page in FireFox and you will receive 2 emails.

You can't visually see it, even with trace statements - you only see the original trace statements, not the ones that would normally appear on a page reload.

We googled this phenonenon and there are issues with FireFox auto-reloading a page with broken images, we just haven't been able to find a resolution.
Avatar of rascal

ASKER

Thanks Gary, I ran the script provided in the link you entered above, and initially it did not return anything. But then I ran the script after a 5 second timer to allow time for tinymce to fully load and insert its bad code and presto - bad links found!

Tinymce is inserting the <a href="#" into the textarea, it looks to be a part of the toolbar code it generates. I'm not sure this is the particular culprit, since it took tinymce a couple seconds to put its hooks in and the page loaded both times almost instantly, I'll try and additional test that sends me an email each time the page loads for final confirmation and let you know.

(see attached screenshot)
tinymce.jpg
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of rascal

ASKER

Hi Gary,

Thanks, but how do you design a page to handle a browser automatically reloading the page twice? This is the problem with Firefox - it is reloading the page with whatever request was made on the page - including on initial entry.
Will depend on your situation but for a scenario.
The page is called, an email is sent - we do not want to send any content to the user so...
At this point redirect back to the page with a flag set so the email code is not run again.

Or you have your email code in a seperate page which is the target of the previous page, once the page has executed redirect to the page you are currently having problems with.

Ergo no email code is executed more than once.

These would be the kind of typical methods when dealing with posted/getted data to prevent duplicate data/functions being run
Avatar of rascal

ASKER

Thanks Gary, I'll definitely fall back to that, but for now I would like to find an alternative to coding such safety checks into each page I ever develop just because of a FireFox bug. I'll see if I can find a less intrusive workaround.
Should be how you always code for pages that deal with posted data or where something is processed like in your case an email

http://murlid05.blogspot.ie/2012/08/avoid-inserting-duplicate-record-on.html
http://en.wikipedia.org/wiki/Post/Redirect/Get
What is actually happening in FF is that it thinks a blank src/# means a call to the same page.
Maybe they will change it but in a way it isn't specifically a bug - in the same way if you leave a FORM action blank then you expect it to just post to the same page...

Edit
Also reference this page
http://brian.pontarelli.com/2006/05/02/is-your-browser-requesting-a-page-twice/

Someone comments on it being actually the correct way it should be handled even tho it kinda doesn't seem right.
if you send the email by javascript, then you may be able to set a cookie wid JS, and read the cookie wid JS, and Not send two emails.
Avatar of rascal

ASKER

It's not an issue of email, I just used the email as a valid way of testing 2 loads of the page for a single request. On the actual site there are lots of pages, each doing different things - displays, database updates, inserts, emails, so I need to come up with a global solution that will protect all of the pages - I am building a script that will inspect a session var for each page to see if it is being reloaded - I'll post it here for reference.
Avatar of rascal

ASKER

Here's a code workaround I wrote that gets inserted at the top of each page via a common file in my template:

//----------------------------------------------
// PageCheck()
//----------------------------------------------
function PageCheck()
{
	global $g_sCurrentPage; // name of the current page such as contactus.php
	
	$fCurrentMicroTime = microtime_float();
	
	$sPageValue = trim($_SESSION[SESSION_PAGECHECK]);
	
	// if page not yet set, return ok
	if (strlen($sPageValue)==0) 
	{
		// set pagecheck session var to current page name and microtime
		$sPageValue = $g_sCurrentPage . ',' . strval($fCurrentMicroTime);
		$_SESSION[SESSION_PAGECHECK] = $sPageValue;
		return;
	}
	
	// $PageValue has something in it so split its values
	$arrPageValue = MySplit($sPageValue, ',');
	$sPageName = $arrPageValue[0];
	$fPageMicroTime = floatval($arrPageValue[1]);
	
	// if we're on a different page, then reset the session var
	if (strcmp($sPageName,$g_sCurrentPage)!=0)
	{
		// different page - reset session var
		$sPageValue = $g_sCurrentPage . ',' . strval($fCurrentMicroTime);
		$_SESSION[SESSION_PAGECHECK] = $sPageValue;
		return;
	}
	
	// we're on the same page - see if the elapsed time is too short (represents FireFox bug of page auto-reloading on empty src=
	if ($fCurrentMicroTime - $fPageMicroTime < 2.0)
	{
		// reset with new value but don't allow to continue
		$sPageValue = $g_sCurrentPage . ',' . strval($fCurrentMicroTime);
		$_SESSION[SESSION_PAGECHECK] = $sPageValue;
		die();
	}
	else
	{
		// it's been more than enough time to ensure that the FireFox reload bug did not reload our page, 
		// so reset our session var and continue.
		$sPageValue = $g_sCurrentPage . ',' . strval($fCurrentMicroTime);
		$_SESSION[SESSION_PAGECHECK] = $sPageValue;
		return;
	}
	
}

Open in new window

Avatar of rascal

ASKER

Hi Gary,
The problem was with TinyMCE - it injects html code in the textareas of our page with dynamic code that includes a src="#". So this causes FireFox to reload the page. It's crazy that FF does this, but the only workaround is to perform the function on the page (in our case, a SQL INSERT) and then redirect to the page again to clear out any parms before the server sends out the <body> tag.

It's only critical with INSERTS, I suppose for UPDATES and DELETES, the fact that the page reloads and performs the same function is not damaging, just overhead. But definitely necessary for INSERTS.