views:

832

answers:

3

Using prototype js library I can access elements by using $(elementID). I can access the element in an Iframe by

$('iframeID').contentWindow.document.getElementById('ID of element inside Iframe').

I would like to use the same dollar method for my Iframe to access elements in Iframe. Is there any way?

A: 

You could alias calling that iframe with something like:

var $IFRAME = function (id){
    return $('iframeID').contentWindow.document.getElementById(id);
}

Then, say you wanted to get the innerHTML of an element in that frame with id 'p1' you could do:

var x = $IFRAME('p1').innerHTML;
alert(x);

Or to manipulate it, for example hide it, you'd do:

$IFRAME('p1').hide();

The $IFRAME name for the function is arbitrary on my part, you could call it getElementInsideIFrameID or whatever appeals to you.

artlung
For example,$IFRAME('p1').hide(); does not work whereas, Element.hide($IFRAME('p1')) will work.What I would like to have that is extending the element inside IFrame to have the same functionality which prototype js does.
Hoque
Adding prototype.js to the "src" of Iframe makes it functional i.e.$IFRAME('p1').hide() works. I have tested it IE and FF. However, if the src IFrame doesnot have prototype.js, it does not work. How this can be achieve without adding prototype.js?
Hoque
In my testing with the latest version of Prototype, locally, calling $IFRAME('p1').hide(); worked for me from inside the iframe containing page. Perhaps set up a test page online and we can get a closer look at what might be happening.
artlung
I have added a test at http://amar-desh.net/stackoverflow/DollarIframe.aspx Please check it.
Hoque
Have a look at: http://lab.artlung.com/change-content-in-iframe-with-prototype/
artlung
I tried at your site but in my Browser IE8, it made error. However, I will make another page in which srcFrame having prototype.js and let me show you that worked for $Iframe('id').hide()
Hoque
Setup another test http://amar-desh.net/stackoverflow/DollarIframe2.aspx where prototype.js also included in the iFrame src file.Thanks.
Hoque
At the moment I don't have IE8 to test with. What is the error?
artlung
Webpage error detailsUser Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.4; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)Timestamp: Thu, 28 Jan 2010 20:44:53 UTCMessage: Object doesn't support this property or methodLine: 15Char: 2Code: 0URI: http://lab.artlung.com/change-content-in-iframe-with-prototype/?fbc_channel=1
Hoque
One thing, I had Prototype being pulled from another domain, I've also changed the code somewhat. Now my example works in both Firefox and Safari. Does it do any better in MSIE?
artlung
Yes, it works by using Elemnet.method not using $('id').method()which I told in my earlier post ...probably you miss that point ..."For example,$IFRAME('p1').hide(); does not work whereas, Element.hide($IFRAME('p1')) will work. What I would like to have that is extending the element inside IFrame to have the same functionality which prototype js does. – Hoque Jan 25 at 3:06 "
Hoque
Sorry I could not help you. Try the Prototype forums -- several options listed here: http://www.prototypejs.org/discuss
artlung
One of my friend has shared the following code and it works like a charm.// Extend prototype's Method avaialble for IFrame Prototype.extendFrame = function(element) { element = $(element); for (var m in Element.Methods) { if (Object.isFunction(Element.Methods[m])) { element.contentWindow.Element.prototype[m] = Element.Methods[m].methodize(); } } }My friend could not tell me the source of the code, but it helps me a lot.Thanks to Artlung for his worthy time and thanks to my friend also.
Hoque
A: 

If you are eager to know the method, you can visit the link (after searching I got the link)

http://www.ruby-forum.com/topic/146705

and for the demo yo can visit here

http://nazmulweb.com/sandbox/stackoverflow/DollarIFrame3.aspx

Thanks.

Hoque
A: 

What is I don't have the name of the iframe? How can I access an element with the name without knowing in which iframe it is?

eniraka