By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

Dynamically setting onload event handlers across frames

Expert 100+
P: 392
Hi,

I am trying to dynamically set an event handler across frames. I have no problems setting properties across frames doing something like

Expand|Select|Wrap|Line Numbers
  1. parent.ToolMenuFrame.location.href = 'http://www.blah blah blah.com';
But when I try to set an event handler this way, as shown below, it fails.

Expand|Select|Wrap|Line Numbers
  1. parent.ToolMenuFrame.window.document.body.onload = "alert('On Load Trigger Fired!!');";
  2. parent.ToolMenuFrame.document.body.onload = "alert('On Load Trigger Fired!!');";
  3. parent.ToolMenuFrame.body.onload = "alert('On Load Trigger Fired!!');";
  4. parent.ToolMenuFrame.window.document.body.onload = "new Function("alert('On Load Trigger Fired!!');")";
  5. parent.ToolMenuFrame.document.body.onload = "new Function("alert('On Load Trigger Fired!!');")";
  6. parent.ToolMenuFrame.body.onload = "new Function("alert('On Load Trigger Fired!!');")";
Has anyone else ever done something like this? Also the DOM docs for both IE and Gecko (Firefox, Mozilla) say I should be able to set the onload handler on the frameset tags as well. Has anyone had any luck doing this?
Nov 14 '06 #1
Share this Question
Share on Google+
6 Replies


iam_clint
Expert 100+
P: 1,208
the one way you didn't try
parent.ToolMenuFrame.window.onload = alert("Onload trigger fired!");
Nov 14 '06 #2

Expert 100+
P: 392
I have tried that one too. No luck.
Nov 14 '06 #3

Expert 100+
P: 392
Does any one know of a way to test if a page has completed loading with out using the onload() event handler? I have not been able to get onload() to work relably inside frames, or when setting it dynamiclly via javascript. As well as problems with it triggering while the page is still loading.

We have been able to get a working option setup using the .readyState attribute of a frame, but it only works in IE.

Does anyone have any other ideas?
Nov 14 '06 #4

Expert 100+
P: 392
the one way you didn't try
parent.ToolMenuFrame.window.onload = alert("Onload trigger fired!");
Ha! I tried this before with no luck, but now it is working!! It must have something with the way I was doing the quotes. Thanks.
Nov 14 '06 #5

P: 1
I am also trying to achieve same kind of functionality in IE.

staying from parent or top frame's (lets say main index container frame) javascript how to call a child frame's (lets say Content frame) onload event. In turn the onload funtion in child frame calls other functions..

Assuming that, I have only one javascript file that is on the top frame.
And it have its own functions on onload event of its own.
I tried to write an onload event from top level javascript as below:

Expand|Select|Wrap|Line Numbers
  1. window.self.onload = function () {
  2.  if(checkCookieExist('uid')){
  3.      //do some
  4.   }
  5. }  //This top level onload works
  6.  
  7. window.top.frames[2].onload = function () {
  8.     alert("Content loaded");
  9.     // do something
  10.   window.top.dynamicallyWriteJS("window.top.frames['ContentFrame']", 'framemodal3.js');
  11. }   //This other frame[0]  onload does not work
  12.  
So, again I am trying to create an onload function in child's DOM in head section dynamically at runtime and trying to call that function all in the top level javascript only..

Expand|Select|Wrap|Line Numbers
  1. function dynamicallyLoadJS(onFrame, jsFilename){
  2.   if(onFrame){
  3. //    var htmlDoc = onFrame.document.getElementsByTagName('head').item(0);
  4.     var htmlDoc = onFrame.document.getElementsByTagName('head');
  5.   }else {
  6. //      var htmlDoc = window.top.document.getElementsByTagName('head').item(0);
  7.     var htmlDoc = window.top.document.getElementsByTagName('head');    
  8.   }
  9.    alert("dynamicallyLoadJS()  : \n frame Title = "+onFrame.document.title);
  10.  
  11.   var jsEle=document.createElement('script');
  12.   jsEle.setAttribute('type','text/javascript');
  13.   jsEle.setAttribute('language', 'javascript');
  14.   jsEle.setAttribute('src', jsFilename);
  15.   htmlDoc.appendChild(jsEle);       //In IE I am getting error here : Object  oest not support this property or method.
  16.   return true;
  17. }
  18.  
But I am failing..
Can't we call different onload funtions on different frames froma single javascript file ?
Please guide me..

Thanks
Shiv
Feb 13 '09 #6

iam_clint
Expert 100+
P: 1,208
You simply use the frame name or id for reference
Expand|Select|Wrap|Line Numbers
  1. <frame id="blah">
  2. <frame id="blah1">
  3. <frame id="blah2">
  4.  
  5. <script>
  6. document.getElementById("blah1").onload = function() {
  7. /*dostuff*/
  8. }
  9. </script>
  10.  
Feb 18 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.