Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get attribute value of parent element from iframe content

Is there a way to get the value of attribute data-media if I'm running the script inside the iframecontent?

Code looks like this:

<div id="myDiv">
    <iframe data-create-resource-url="http://my.domain.url" 
       data-media="Song" 
       frameborder="0" 
       height="41" 
       src="https://different.domain.url" width="366">
     </iframe>
</div>

I've tried a lot of ways already like window.parent.document or top.document or window.parent and other available solutions but doesn't seem to work.

like image 262
MoMo Avatar asked Feb 17 '23 05:02

MoMo


2 Answers

Well you dont really need to access the parent. As you are running the script inside iframe. iframe is currently the window for your script inside iframe. So accessing window element should give you the said attr. Try this (not tested):

alert($(window).attr('data-media'));
like image 149
Jehanzeb.Malik Avatar answered Feb 22 '23 00:02

Jehanzeb.Malik


Did you tried this:

window.frameElement.getAttribute("data-media");
like image 38
pjehan Avatar answered Feb 22 '23 01:02

pjehan