Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an element outside of iframe with javascript or jQuery?

I'm trying to get a element from outside of iframe with js or jQuery. I found something but it's not cross browser.

<div id=vi-desc-maincntr"">
    <div class="u-flL iti-act-num">123456789</div>
    <div id="desc_div">
        <iframe src="LOCATION">
    </div>
</div>

I need that number into a variable.

like image 959
Adrian Axinte Avatar asked May 28 '14 14:05

Adrian Axinte


People also ask

How can I access iframe element with JavaScript?

Getting the element in IframegetElementById() method by passing iframe id as an argument. const iframe = document. getElementById("myIframe"); Now, it has and contentWindow property which returns the document object by using that we can access the elements from an Iframe.

How can get iframe HTML using jQuery?

# jQuery Code to Get HTML Content of IFrame contents(). find("html"). html()); }); The above code will return all the HTML content of our Iframe.

How do I check if an element is an iframe?

In short, to check if a page is in an iframe, you need to compare the object's location with the window object's parent location. If they are equal, then the page is not in an iframe; otherwise, a page is in an iframe.

Can you iframe a specific part of page?

If you want to display specific area of external website using iframe, then you will learn in this article how to embed a specific part of a web page using css. Suppose, you want to show only one div element from another website and want to hide some specific content, then you can easily do this using <iframe> tag.


1 Answers

Same-origin policy:
You might want to check this. Two pages need to have the same origin.
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy


If your child page is located at same domain as parent page, You can write a code like below in the child window :
 $('#test1', parent.document).html('<h1>clicked</h1>');

The second parameter provides the context in which to search the element matched by the first parameter. The Document is here:http://api.jquery.com/jQuery/#jQuery-selector-context

 jQuery( selector [, context ] )

Hope this helps.

like image 135
naota Avatar answered Nov 08 '22 15:11

naota