tags:

views:

452

answers:

2

Apologies if this is already answered elsewhere, but I've been searching all morning and not found a solution, so I'm hoping someone can help.

So - from javascript I am opening an iframe

righttop.location = "timesheet_notes.php";

and then wanting to pass information to it:

righttop.document.notesform.ID_client.value = Client;

Obviously though, that line isn't going to work until the page has fully loaded in the iframe, and that form element is there to be written to.

So, what is the best/most efficient way to address this? Some sort of timeout loop? Ideally I would really like to keep it all contained within this particular script, rather than having to add any extra stuff to the page that is being opened.

I'd really appreciate some advice - very many thanks in advance!

+1  A: 

First of all I believe you are suppose to affect the src property of iframes, not location. Second of all hook the iframe's load event to perform your changes:

var myIframe = document.getElementById('righttop');
myIframe.onload = function() {
    this.contentWindow.document.notesform.ID_client.value = Client;
};
myIframe.src = 'timesheet_notes.php';

Again, this is all presuming you mean iframe, not framesets.

Crescent Fresh
Thanks Crescentfresh - that is absolutely what I was looking for; simple, elegant, and works a treat! :-)
Bill Dawes
Is there any way to do this by attaching an event? Let's say that the iframe may have it's own onload handlers and I don't want to accidentally clobber them by reassigning the onload property.
Eric Nguyen
@Eric: yes. Use a library like Mootools, Prototype 1.6 or jQuery. If you don't have one of those, use this: http://ejohn.org/blog/flexible-javascript-events/
Crescent Fresh
Ah... Does that mean that this onload function in the script that triggers the page load, might interfere with an onload function in the page that has been loaded? I guess as long as they are not both trying to write to/from the same element it will be alright?
Bill Dawes
@Bill: no, @Eric's question was about whether this answer will interfere with any `onload="..."` attribute set on the `<iframe>` element itself. The answer to that is yes, it will. However any `onload` handler set *within* the iframe document is not affected.
Crescent Fresh
+1  A: 

I guess you can pretty easily do this with jQuery... jQuery Home Just hook the page to the jQuery $ function ()... for e.g

$(document).ready(function() { $('iframe').load(function() { // write your code here.... }

Have a quick look at the file uploader example here.. Using iframes for multiple file uploads...

Please check whether this is useful to you...

rajesh pillai
I didn't really want to get into jquery (haven't opened that door yet), I was hoping just to deal with it in a few elegant lines of javascript. But thanks anyway Rajesh!
Bill Dawes
not an issue.. thanks for posting as i got to learn a few tricks without using jquery :)
rajesh pillai