I though this would be simple enough but I can't find the answer. I need to know how I can access an iframe's name from within said iframe. I was trying something like this but it's not working.
<iframe name="thename">
<script type="text/javascript">
alert(parent.name);
</script>
</iframe>
You were nearly right. Setting the
name
attribute on aframe
oriframe
sets thename
property of the frame's globalwindow
object to that string. (Notparent
, which refers to the window of the document that owns the frame.)So unless some other script has deliberately changed the name, it's as simple as:
Well, an IFRAME element shouldn't contain anything, it's targeting another document. So using a SCRIPT tag inside an IFRAME doesn't make alot of sense. Instead, use the SCRIPT inside the called document, e.g.
iframe_caller.html:
iframe_doc.html:
Note I'm using
parent.document.function()
there.Something like this should work:
You have to use the parent and then get the element either byId, Name, etc... then access the name property.
So your code should be like:
in some cases
window.frameElement
returnsnull
in iframe, so i figured workaround for it.1. you need to set hash in src url of iframe
2. in iframe you can get this hash with
There is a combination of answers I prefer:
It works on IE7+, Mozilla & Chrome