How to Check if an iFrame Has a Value In It - javascript

I have the following iframe within a parent window:
<iframe
name="sg_iframe"
id="sg_iframe"
src="www.myurl.com"
align="left"
height="150px"
width="750"
frameborder="0"
marginheight="0"
marginwidth="0"
overflow="auto">
</iframe>
Now within www.myurl.com, let’s say I have a report that display 3 fields based on some criteria.
So based on this, how can I check from the parent form whether the iframe described above is empty of data, i.e. no report data listed or actually does have data using jquery/javascript?
Basically just need to know if the report within the iframe has 0 rows or 1 or more rows.
Thanks.

If it's an internal URL, you can use jQuery's load to get the page's content.
If it's an external URL, you can't.

$('iframe').each(function() {
if($(this).contents().find('#content').text() == '') {
$(this).addClass('warning');
}
});

Related

check into html code for a iframe inside custom tags

I am trying to get a text inside a tag in a html of an iframe inside another custom tag.
For example, as shown in the pic, the force-record-layout-section is a custom tag inside a div and there is a iframe that I am trying to get element from.
But it seems when the iframe is inside the custom tag, neither jquery nor Document.GetElementsById/Name/Class/Tag are returns anything instead , all returned undefined.
var frame = document.getElementById("matchClassicStyleText");
console.log("iframe "+frame);
It just logged iframe undefined in the console.
When using jquery, it can locate the custom tag, but .html() returned empty.
Please help me understand if anything I have done wrong or there is other workaround.
The iframe
<iframe force-alohapage_alohapage="" height="220px" width="100%" scrolling="yes" allowtransparency="true" name="vfFrameId_1641796894366" title="accessibility title" allowfullscreen="true" lang="en-US" allow="geolocation *; microphone *; camera *" tabindex="0"></iframe>
Tried document.getElementsByName("vfFrameId_1641796894366") and it is also not working.
matchClassicStyleText is one of the div inside the loaded html of the iframe.
Thanks.

Get length of contents inside iframe and modify display based on result

I want to write some jQuery code to get the content length on an element whose id="newUp" from an iframe, and if length == 0 then show the element whose class="OE" from the page:
function onframeload(){
if($( "iframe:contains('.new')" ).length > 0){
$('.oe').show()
}
};
I am a beginner in javascript.
Main page code is:
<div style="display:none;" class="oe">CHAL PEA OE!</div>
<iframe onload="onframeload();" src="//jsfiddle.net/ROYALRandhawa/Lysn9e5z/embedded/result/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
This is the code in the iframe:
<div class="news"><center>Welcome Boss</center></div>
<span id="newUp">Here is new update</span>
There's a security mechanism in browsers that prevents you from performing tasks in the iframes that doen't belong to the same domain
Take a look at this:
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

Get the URL of an iframe then alert it

So basically I'm trying to make a button so when you press it, it alerts the url the iframe is on.
My HTML:
<iframe id="iframeid" scrolling="auto" width="100" height="100" src="http://www.google.com" >
</iframe>
<button onclick="myFunction()">Click me</button>
Then for my Javascript I have:
function myFunction() {
alert(document.getElementById('iframeid').contentWindow.location.href);
}
Though when I press it it's not working. When I replace the alert with something else like "Potato" then it will work. Though for some reason it can't get the url, or maybe the frame. Any help accepted!
When the frame is displaying a document on another origin (as in your example), there is no way to get the URL that is being displayed.
The URL might include personal data belonging to the user so your site is prevented from accessing it.
Always look at your JS console:

Iframe Http error handling

I am using iframe to show my database result.But for the veryfirst time since I am not hitting the database so in that case iframe is showing datatable.jsp page is not available (dataTable.jsp is the page for showing database table result).I searched in google and i found something called onError and onLoad methods for iframe.If anybody can show me a small example of how to show a different jsp if for the first time required src is not avaialable it would be a great help for me.
Thanks in advance
<iframe id="dataframe" src="dataTable.jsp" name="dataTable" width="720px" height="620px" align="middle" frameborder="0">
</iframe>
Well, with javascript, this could be a way:
HTML:
<iframe id="dataframe" name="dataTable" width="720px" height="620px" align="middle" frameborder="0">
</iframe>
(removed the src attribute)
Then, when you want to load the datatable with your jsp content
JS:
document.getElementById("dataframe").src = 'dataTable.jsp';
Hope this helps, Cheers

capture target link on iframe

Ok, thank you first of all his attention. I have three iframes in 3 different html documents. Organized in this way:
In iframemain.html I have:
<iframe src="iframeparent.html" width="100%" height="600">
</iframe>
In iframeparent.html I have:
<iframe src="iframeson.html" width="90%" height="350" name="_parent">
</iframe>
In iframeson.html I have:
<iframe src="http://anywebsite.com/samplepage.html" width="80%" height="300">
</iframe>
I did this because http://anywebsite.com/samplepage.html links are loaded into main window (iframemain.html), and not what I want, I wish I could capture the target and load the content iframeparent.html without afect to iframemain.html
I clarify that I have no control over the content of: http://anywebsite.com/samplepage.html, but it is annoying that your links loaded into the main window, so I would like all of those links are loaded in the same iframe or much in the iframeparent.html
Is this possible with Javascript or Jquery?. Thanks for your answers.
See if I have understood this correctly
You want to frame a page http://anywebsite.com/samplepage.html on iframemain.
That page has a bunch of links with target="_parent" or worse, "_top"
You want the page you have framed to not target iframemain but to have all links stay in that frame.
If the links target _parent, then your solution is correct. If they target _top, you would need to use a proxy on the server. It would break if they change the way they target links (using javascript or base target for example) but the idea is that you have
<iframe src="yourproxy.php?url=http://anywebsite.com/samplepage.html" width="80%" height="300"></iframe>
and in there, replace all _top and/or _parent with _self
But show some examples of the links (you can change the URL to protect your client) so we can help better

Categories

Resources