Solved

Javascript window.onload not firing in Firefox

Posted on 2010-11-08
7
956 Views
Last Modified: 2016-12-02
Hi Experts -

I have the following code set up to repopulate some product filters that I am storing so that when the user presses the back button or refreshes the screen, the filters are saved.

The code works perfectly in IE when I press the back button or rerfresh the page, the filters are restored.

In firefox this only fires if I refresh the page. If I press the back button, the javascript never fires.

This may not be the best way to do  this. I am open to suggestions if there is a better way to program the javascript.

Thank you!


<script type="text/javascript">

function loadProducts(type,category)
{
      var brand = [];
	  var color = [];
	  var price = [];
	  var capacity = [];
	  var width = [];
	  var val = [];
	  var COOKINGTYPE = [];
	  var POWERSOURCE = [];
	  var BURNERTYPE = [];
	  var SURFACETYPE = [];
	  var COOKINGSURFACE = [];
	  var CLEANINGTYPE = [];
	  var SHELVING = [];
	  var MOTORSPEEDS = [];
	  var HORSEPOWER = [];
	  var TYPEFEED = [];
	  var TUBMATERIAL = [];
	  var CONTROLTYPE = [];
	  var WASHLEVELS = [];
	  
	  
	Url = "products_filter.php?Category="+category+"&Available=1&Search.x=50&Search.y=8";
	Datas = $("#frm").serialize();
	$.ajax({
		url:Url,
		type:'POST',
		data:Datas,
		beforeSend: function ()
		{                               
			$("#product").html("Please wait...");                                               
		},
		success:function(msg)
		{                     
			$("#product").html(msg);                 
		}                   
	   });

}

function nav_ajax(page)
{
    var type='price';
	//$.post(page,{},function(data) { $("#content").html(data);});
	 var brand = [];
	  var color = [];
	  var price = [];
	  var capacity = [];
	  var width = [];
	  var val = [];
	  var filters = [];
  
	
	Url = page;
	Datas = $("#frm").serialize();
	$.ajax({
		url:Url,
		type:'POST',
		data:Datas,
		beforeSend: function ()
		{                               
			$("#product").html("Please wait...");                                               
		},
		success:function(msg)
		{                     
			$("#product").html(msg);                 
		}                   
	   });
}
//-->

  </script>
  <?php if(isset($_SESSION['jac1']))
  {?>
  <script language="javascript" type="text/javascript">
  window.onload = function() {loadProducts('brand','<?=$_GET['Category'];?>');};
  </script>
  <?php }?>
	<?php if(isset($_SESSION['jac2']))
  {?>
  <script language="javascript" type="text/javascript">
  window.onload = function() {loadProducts('color','<?=$_GET['Category'];?>');};
  </script>
  <?php }?>
  <?php if(isset($_SESSION['jac3']))
  {?>
  <script language="javascript" type="text/javascript">
  window.onload = function() {loadProducts('price','<?=$_GET['Category'];?>');};
  </script>
  <?php }?>
  <?php if(isset($_SESSION['jac4']))
  {?>
  <script language="javascript" type="text/javascript">
  window.onload = function() {loadProducts('cap','<?=$_GET['Category'];?>');};
  </script>
  <?php }?>
  <?php if(isset($_SESSION['jac5']))
  {?>
  <script language="javascript" type="text/javascript">
  window.onload = function() {loadProducts('width','<?=$_GET['Category'];?>');};
  </script>
  <?php }?>

Open in new window

0
Comment
Question by:danjen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 7

Expert Comment

by:dxdinh
ID: 34089354

-- try this  from

http://dean.edwards.name/weblog/2005/09/busted/

// for Mozilla browsers

if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

or his method
http://ckon.wordpress.com/2008/07/25/stop-using-windowonload-in-javascript/
0
 

Author Comment

by:danjen
ID: 34089363
Thank you for your reply. Where on the page would I put the above code? Can you show an example?
0
 
LVL 7

Expert Comment

by:dxdinh
ID: 34089375
--  since you using jQuery - i would add this in your document.ready

<script type="text/javascript">                                        
   $(document).ready(function() {
        
// for Mozilla browsers

if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}      

   });
</script>
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:danjen
ID: 34089393
Sorry - I am not very familiar with Javascript.  Should I just copy and past this above the code I have hear or does it go in the head tags?

I pasted your code in to the head tag but it did not help.
0
 
LVL 7

Expert Comment

by:dxdinh
ID: 34089516
you can try to replace all your instance of window.onload
such as:
 window.onload=your_function;

to
if (window.attachEvent)
  {window.attachEvent('onload', your_function);}
else if (window.addEventListener)
  {window.addEventListener('load', your_function, false);}
else
  {document.addEventListener('load', your_function, false);}

 i know it's longer but it will take care all the browser for you
0
 
LVL 7

Accepted Solution

by:
dxdinh earned 500 total points
ID: 34089545
something like this if you not clear

replace this
      window.onload = function(){loadProducts('brand');};

with this      

if (window.attachEvent)
      {
                  window.attachEvent('onload', loadProducts('brand') );
      }
      else if (window.addEventListener)
            {
                  window.addEventListener('load',loadProducts('brand'), false);
            }
      else {
            document.addEventListener('load', loadProducts('brand'), false);
      }
0
 

Expert Comment

by:Mehmet Erten
ID: 41911079
the following code works in between <body> ... </body> in Edge/Firefox/Chrome
but when I moved them into a javascript.js file it does not... not happening if not after the load time.. back to the problem again. Catch-22

<script>

if (window.attachEvent)
{
    window.attachEvent('onload', loadMyEvents() );
}
else if (window.addEventListener)
{
    window.addEventListener('load',loadMyEvents(), false);
}
else
{
    document.addEventListener('load', loadMyEvents(), false);
}
 
function loadMyEvents()
{
    document.getElementById("myname").onfocus = function(){doOnFocus(this)};
    document.getElementById("optin").onclick = function(){doOnClick(this)};
    document.getElementById("myname").onblur = function(){doOnBlur(this)}
    document.getElementById("mydirection").onchange = function(){doOnChange(this)};
    document.getElementById("myform").onsubmit = function(){return doOnSubmit(this)};
    document.getElementById("windowsBody").onload = function(){doOnLoad()};
    document.getElementById("mylink").onclick = function(){doOnClick(this)};
    document.getElementById("mypara").onmouseover = function(){doOnMouseOver(this)};
    document.getElementById("mypara").onmouseout = function(){doOnMouseOut(this)};
}    
</script>

What is the trick to make the function only works after the onload in an external js file.

The following worked on all three browers (Chrome, Firefox, Edge): (I try not to use any jQuery):

window.onload = function() {myFunction()};
 
function myFunction()
{
    doOnLoad();
    document.getElementById("myname").onfocus = function(){doOnFocus(this)};
    document.getElementById("optin").onclick = function(){doOnClick(this)};
    document.getElementById("myname").onblur = function(){doOnBlur(this)}
    document.getElementById("mydirection").onchange = function(){doOnChange(this)};
    document.getElementById("myform").onsubmit = function(){return doOnSubmit(this)};
    document.getElementById("mylink").onclick = function(){doOnClick(this)};
    document.getElementById("mypara").onmouseover = function(){doOnMouseOver(this)};
    document.getElementById("mypara").onmouseout = function(){doOnMouseOut(this)};
}
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question