View Full Version : setfocus?
doomWAVE
06-08-2005, 06:27 PM
how i can setfocus an a iframe?
my frame name is hedef.
Philip M
06-08-2005, 08:36 PM
You can't as far as I know. You can only place the focus on an input textbox.
brandonH
06-08-2005, 10:26 PM
wrong, you can set focus to things other than input items.
you can set focus to anything that has an id.
does your iframe have an id?
if not you need to set one.
<iframe id='hedef'>
once you do that all you need to do is call to a script:
document.getElementById('hedef').focus();
hope this is what you wanted
I guess that both of you are wrong and somehow both of you are right.
focus() method is a "polymorph" method, thus it can act upon form's elements and upon window object as well. That means if you want to focus the page in iframe, you must focus the window which is loaded inside the iframe, not the iframe object. (only IE will focus the iframe object, only God and Bill gates know why... :D )
So that, to focus a page in an iframe, the crossbrowser correct solution is:
top.frames['iframename'].window.focus();
glenngv
06-09-2005, 11:41 AM
I guess that both of you are wrong and somehow both of you are right.And also you. :D
It depends on how you access the iframe.
If via getElementById,
document.getElementById("iframeId").focus() will work in IE but not in Firefox.
To make it work for both browsers, you need to do this:
document.getElementById("iframeId").contentWindow.focus()
If accessed via frames collection,
window.frames['iframeName'].focus() will work for both browsers
window.frames['iframeName'].window.focus() will also work but it is redundant.
In other words, IE is consistently working whatever way you access the iframe but Firefox acts a little bit choosy.
Try this in IE and FF to see the difference:
<iframe id="ifrId" name="ifrName" src="dummy.htm"></iframe>
<input type="button" onclick="alert(window.frames['ifrName'].focus)" value="via frames" />
<input type="button" onclick="alert(document.getElementById('ifrId').focus)" value="via getElementById" />
<input type="button" onclick="alert(document.getElementById('ifrId').contentWindow.focus)" value="via getElementById/contentWindow" />
And also you.
:p
If my solution is only one of the possible solutions, that not means I am wrong. I know that if the object is window, that specification is redundant, but I wanted to make clear that the object to be focusd is the window object, not the iframe object, that is all
glenngv
06-09-2005, 12:08 PM
I was saying you are wrong and right at the same time, the same way you told the other two posters here. :p
but I wanted to make clear that the object to be focusd is the window object, not the iframe object, that is allBut iframe (and frame for that matter) is essentially a window object. Only thing is there is a special case for iframe as disscussed in my previous post.
But iframe (and frame for that matter) is essentially a window object.
With all the respect, I would not say so.
The iframe object is different by the window object loaded in that iframe. Need examples? ;)
glenngv
06-09-2005, 02:07 PM
My examples in my previous post of accessing iframe via frames collection prove that iframe is essentially a window object. It does not become a window object in Firefox if you access iframe by getElementById.
iframe is essentially a window object
I would rather say that it is an interpolator object between parent window and framed window. It is an object (iframe) in itself which contains another object. The contained object might be a picture as well.
If iframe object would have been a window object, it would have had accepted all the usual window methods, which is far from true. AFAIK it suppports only onload window's event. Even so, the iframe element reference and the window loaded in iframe reference are different, so that, logically, they are different objects.
glenngv
06-10-2005, 03:24 AM
If iframe object would have been a window object, it would have had accepted all the usual window methods, which is far from true. AFAIK it suppports only onload window's event.
This demo page will speak for itself. It dumps the methods, properties and event handlers of IFRAME accessed in 3 different ways. Try it.
<html>
<head>
<title>Supported methods and properties of IFRAME</title>
<style type="text/css">
td, th {vertical-align:top;padding:2px;}
body,table {font:11px Verdana;}
#note {font-style:italic;color:red;}
</style>
<script type="text/javascript">
function dumpIframe(oIframe, contentId){
var str="";
for (var item in oIframe){
if (typeof oIframe[item] == "function")
str += "<b>" + item + "</b> = [function]<br />"; //for Firefox
else if (item.indexOf("on") == 0)
str += "<b>" + item + "</b> = [event handler]<br />"; //for IE
else
str += "<b>" + item + "</b> = " + oIframe[item] + "<br />";
}
document.getElementById(contentId).innerHTML = str;
}
function init(){
dumpIframe(window.frames['ifrName'], 'column1'); //via frames collection
dumpIframe(document.getElementById('ifrId').contentWindow, 'column2'); //via document.getElementById(...).contentWindow
dumpIframe(document.getElementById('ifrId'), 'column3'); //via document.getElementById(...)
}
window.onload = init;
</script>
</head>
<body>
<iframe name="ifrName" id="ifrId" src="about:blank"></iframe>
<h1>Supported methods and properties of IFRAME</h1>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th>via frames[ ] collection</h>
<th>document.getElementById(...).contentWindow</h>
<th>document.getElementById(...)</h>
</tr>
<tr>
<td style="width:33%">
<div id="column1"></div>
</td>
<td style="width:33%">
<div id="column2"></div>
</td>
<td style="width:34%">
<div id="column3"></div>
</td>
</tr>
</table>
<div id="note">
*NOTE: IE doesn't dump methods while Firefox does. IE dumps event handlers while Firefox doesn't.
But of course, methods and event handlers are supported by both browsers.
</div>
</body>
</html>
Anyway you haven't entirely coaxed me. I don't see how you can treat the iframe object as a "real" window object. The real window is that which is loaded in the iframe using SRC attribute, If no SRC, the iframe still exists as an object on the page, but it has nothing to display.
glenngv
06-10-2005, 03:12 PM
But the results showed the exact opposite of what you said. It listed the supported methods, properties and event handlers of Iframe which are the same with window's.
its shoes the results for the
iframeobject.contentWindow object (which is a window object)
not of the
iframeobject which I sense rather like a document's object.
Anyway, I agree that most of the refrences I have seen indicates iframe (and frameset) as a window object, but the argument is that almost all the window's method may be applyied uppon an iframe. But, logically talking, this is a sophisme like:
The plain is green, the lizzard is green, thus the plain is a lizzard.
If an object might accept some other typed object's methods, I guess that it is not compulsory that the first object is similar with the second.
glenngv
06-10-2005, 03:22 PM
It showed 3 different ways of accessing iframe. If you really analyzed the results, you can see that if the iframe is accessed via frames collection (window.frames['iframeName']), the iframe object has almost the same methods, properties and event handlers compared to iframe accessed via document.getElementById('iframeId').contentWindow.
glenngv
06-10-2005, 03:32 PM
Your argument was iframe is not a window object. And I was saying iframe is essentially a window object.
window.frames['iframeName'] is an old reference method. After all, even document.all (for IE) would have reference all the objects on the page, no matter the type...
I'll think about, anyway.
glenngv
06-10-2005, 03:41 PM
The issue here is not being an old or new method but what "type" of object iframe is.
"Frame or object?" :D
http://www.quirksmode.org/js/iframe.html
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.