2011-05-03 4 views
17

J'ai une page qui a un iframe. Chaque fois que la page se charge, iframe reçoit un identifiant et un nom uniques. Je peux obtenir le nom de IFRAME iframe chargé comme ceci:Javascript: get iframe id dans la page chargée

alert(parent.window.frames[window.name].name); 

Mais quand je tente d'obtenir l'identifiant:

alert(parent.window.frames[window.name].id); 

-je obtenir undefined?

Est-il possible d'obtenir l'attribut id de l'iframe dans la page chargée?


http://jsfiddle.net/cqFtB/


<iframe id="lyygi8stwZSANUEh" src="http://example.com" name="zma82vRVe18xbAqW" title="Awesome Iframe"> 

example.com:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Awesome Iframe</title> 
</head> 
<body> 
    <script type="text/javascript"> 
     alert(parent.window.frames[window.name].name); 
    </script> 
</body> 
</html> 

Répondre

45

Essayez window.frameElement.id dans le iframe.

+0

Voici un lien http://jsfiddle.net/cqFtB/ – Pav

+0

qui précède donne l'erreur suivante: 'Permission denied accéder propriété « id '' – Pav

+0

Cela peut arriver si l'iframe et la page qui le contient proviennent d'origines différentes. Sont-ils dans votre cas? –

0

Vous devez insérer le nom de l'iframe:

alert(parent.window.frames['zma82vRVe18xbAqW'].name); 
0

Dans le iframe cela fonctionne pour moi. Mais seulement si l'iframe et la part des parents du même domaine:

this.frameElement.attributes.id 
Questions connexes