Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runtime error onLoad (using frameset) 2

Status
Not open for further replies.

stewartwebb

Programmer
Jan 6, 2004
92
GB
Hi All,
I have a problem with this page that is using a frameset. When the 2 frames load, onLoad in both frames calls a function which then displays a runtime error. The error doesn't appear everytime the page loads, it's very random, so i'm hoping someone can help.
In both the frames onLoad i'm calling a function called funcSetBannerInfo().

Code:
function funcSetBannerInfo()
{
if(window.top.window.frames[0] != null)
    {
	if(window.top.window.frames[1] != null)
	    {
        setMainTitle();
		
		checkDEMO();

        window.top.window.frames[0].window.document.getElementById('Company').innerHTML = "";
        window.top.window.frames[0].window.document.getElementById('Company').innerHTML = window.top.window.frames[1].window.document.form1.COYNAME.value;
		}
	}
}

The reason i'm calling it from both frames is that the last frame that loads will perform the function, this is why i've got the frame check null at the beginning of the function.
This is when I get the runtime error:-

Code:
line 133    window.top.window.frames.0.window.document.getElementById(.'   is null or not an object

The function that is used in line 133 is as follows:-

Code:
function setMainTitle()
{
window.top.window.frames[0].window.document.getElementById('MainTitle').innerHTML = "";

for (i=0; i<window.top.window.frames[1].document.form1.elements.length; i++)                         
    {
    if (window.top.window.frames[1].window.document.form1.elements[i].name == "MAIN_TITLE")
        {
		window.top.window.frames[0].window.document.getElementById('MainTitle').innerHTML = window.top.window.frames[1].window.document.form1.elements[i].value;
		break;
		}
	}
}

This function setMainTitle() is called from funcSetBannerInfo().

Line 133 is the first line of code in the fuction(setMainTitle()).

As I said this is random and doesn't happen everytime.
Does anyone have any ideas why this is happening and how I can solve it?

Thanks

Stewart.
 
First, what is the error that you get? It could indicate a problem other than how/when the function is called.

Does the function exist in both pages being loaded in the frames or only the main page?

If the two frame pages are conflicting with each other and you cannot find a way to have the call work from just one then try setting a value test in the function.
For instance, have x=0 by default. When the function is called test to see if x>0 and if not then increment x and exit the function. If it is greater then you know the function is being called for the second time and can let it go through to completion.


At my age I still learn something new every day, but I forget two others.
 
you may get the error if the elements in the frame you are trying to interact with have not fully loaded to the page when you try and access it.

You may be better off checking if each element exists as you step through the tree and if not put a timeout in and then retry until the page is fully loaded/rendered.

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005
 
Good point ggriffit. I did not look at the code to see if the elements affected were in a part of the page where that might be an issue thinking that with an onload event it would not be necessary. Of course it will if each frame has it's own event and refers to an element on another frame.

This should be a non-issue though if the function is set to only allow execution on it's second call. Unless of course the element affected is in yet another frame that might not have completed loading.


At my age I still learn something new every day, but I forget two others.
 
Thanks for the replies.

I've changed the code as ggriffit suggested and now the function is called after 2 seconds. This seems to be enough time to allow both frames to load before the function is called.

Thanks both for your help.

Stewart.
 
stewartwebb, I still say you would be best off setting a flag so you will know when the 2nd call to the function occurs. A delay will do just that, delay. If for some reason the page takes an unusually long time to load though you could still run into the same problem. And we know how often pages can load slowly.

If you use a test for the 2nd call then you do not have to have any execution delay and you will not run into an instance where a slow loading page causes it to fail.

Example:
Code:
var pass2 = 0;
function myFunc() {
  if (pass2 == 1) {
    do something here.
  }
  else {
    pass2 = 1;
  }
}

The first time the function is called pass2 would be 0 so the commands in the if statement would not execute. Then pass2 is changed to 1 so the next call it will evaluate true and the lines will execute.
There are many ways to do this but essentially you are just setting a flag so that the lines only execute after the second call to the function.


At my age I still learn something new every day, but I forget two others.
 
Thanks theniteowl,
I have tried this but came into a problem. The top frameset is just a page that is opened when users login and then from the frameset it opens the banner and the main page. The global javascipt file is not in the frameset page it's in the banner and main page and so when the function is called pass2 is 0 as the javascript file is effectively opened twice and independent of eachother.
The frameset and the banner are then kept open for the whole of the users session and it is the main frame that is submitted to change the page.
Do you have any other ideas for this, sorry if I didn't make the scenerio clear in the first place.

Stewart.
 
theniteowl,

I put the function in the main frameset and it works much better, thanks.

Stewart.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top