Howdy guys, im having trouble finding help on creating a callback in certain situations.
I have a piece of code which loads a links page in to an iframe and then changes the scr if another link is pressed.
$(".iframe").hide();
$(".lnk").click(function(){
$(".iframe").show('slow')
;})
;
$(".frmclose").click(function(){
$(".iframe").hide('slow')
;})
;
The above runs within (document).ready
below is outside of this (for some reason it does not work on the inside)
function changeIframeSrc(id, url) {
if (!document.getElementById) return;
var el = document.getElementById(id);
if (el && el.src) {el.src = url;return false;}return true;}
the link :
google
prior to this i have the div in which the iframe is in become unhidden. I also have a button within that div which hides the div + iframe.
What im having problems with is once the iframe has been opened and then closed via the div link if it is re-opened by clicking a different link the iframe unhides to display the old page then changes. But what i want is for the frame to load the new page(while hidden) then unhide to display it. I thought of using a callback after the iframe src change but i cant see where i would implement it.
Example of it happening
(click the GDPH button to see the links for the iframe)
Any thoughts or help appreciated.
Regards
B Stoner
I think that all you need to do is clear the src of the <iframe> when it is closed. That will clear the page so that next time you show the iFrame it will start out blank again.
I made a small demo of this functionality that uses the 3 links from your page as an example. The <iframe> starts hidden (by CSS) and each link will show the <iframe> and then load the remote site. I added a close iframe link to simulate the close link you have under the <iframe> on your site.
Hope this helps!
Edit: Updated the demo link to include the callback part. Somehow missed that when I read the question!
Edit 2: Your changeIframeSrc function was not working was because it was defined inside the jQuery anonymous function and is a closure. See calling Jquery function from javascript
I would catch the .load() event for the iframe, this will fire after the document has been loaded in to the iframe.
$(".iframe").load(function { /* code to run when iframe is loaded */ });
Related
I have an iFrame which contain an anchor with target="_blank". I wish to run some JavaScript in the iFrame after the new window has opened.
I tried using setTimeout(/*my js to run*/, 0) in the onclick event of the anchor. It works in Chrome but not in Firefox. Putting a 100 ms delay in the setTimeout seems to fix this issue with Firefox, but this solution does not look very clean.
Is there a standard way to run js code after a link was opened in a new window?
edit some clarifications: the js I want to run triggers a page refresh. If I put it directly in the onclick event, the anchor link is not opened because the onclick event happens before. This is why I need to run the js code after the new window has opened.
second edit I have access to the first page and the popup, but not to the target of the link.
If you need to refresh AND open a link in a new window you do want setTimeout like this
Inline:
<a href="popup.html" target="_blank"
onclick="setTimeout(function() { location.reload(1);},10)">Click</a>
Better:
Click
using
function reload() {
setTimeout(function() { location.reload(1);},10);
}
window.onload=function() {
document.getElementbyId("pop").onclick=reload;
}
Do you have access to the page in the popup and is it the same origin as the iFrame and is it the same origin as the parent page? Then you can use the onload event of the popped page:
window.onload=function() { opener.reload(1); }
Add an onReady or onLoad hook into the page that's loading. Then, "after the new window has opened" the JavaScript will be run. If you need to affect the parent window then use window.opener in the JavaScript in the popup.
I am trying to create a bookmarklet functionality similar to the thefancy.com site, I created a bookmarklet button which when clicked loads a js file which is located on some other website http://wonderstreet.localhost.com and append it in the head section of the current document, it then creates a iframe and append it in the body of the current document.
The above mentioned js file contains various functions which I need to call from this iframe, for example, there is a "Close" button in the iframe which when clicked should call the function which is located in the above js file (this function will close or remove the iframe)
The iframe gets created and displayed correctly.(I need to fetch all the images of current document and show it in iframe)
Problem is that when I click close button it says: ReferenceError: function is not defined
Can somebody please help and let me know how can I solve this ?
[I am using core javascript and not jquery]
Here is the code from the js file =>
function create_bookmarklet_iframe(thewonderstreet_userid) {
var ifra=document.createElement('iframe');ifra.src="http://localhost.com/index.php/image_picker?userid="+userid;ifra.setAttribute("id","bookmarklet_iframe");ifra.setAttribute('allowtransparency',true);ifra.setAttribute('style','width:279px;height:372px;border:1px solid #4c515c;position:fixed;top:10px;right:10px;z-index:10000001;margin:0;background-color:#eff1f7;');void(document.body.appendChild(ifra));
}
function remove_bookmarklet_iframe(id) {
var element = document.getElementById(id);
element.parentNode.removeChild(element);
}
And this is the bookmarklet code:
javascript:%20(function%20()%20{%20%20%20%20%20userid%20=%20'724c5a0e49e4dac588a90e17233982493027197d';%20%20%20%20%20var%20search_url%20=%20'http://localhost.com/javascript/bookmarklet_js.js';%20scripts_finder%20=%20document.getElementsByTagName('script');%20var%20found_url%20=%200;%20for%20(var%20i%20=%20scripts_finder.length;%20i--;)%20{%20var%20actual_url%20=%20scripts_finder[i].src.split("?");%20if%20(search_url%20==%20actual_url[0])%20{%20found_url++;%20}%20}%20if(found_url%20<=%200)%20{%20s=document.createElement('SCRIPT');s.type='text/javascript';s.src='http://localhost.com/javascript/bookmarklet_js.js?_='+(Math.random());document.getElementsByTagName('head')[0].appendChild(s);%20}%20%20var%20s_id%20=%20'bookmarklet_iframe',%20%20%20%20%20%20%20%20%20s_avail%20=%20document.getElementById(s_id),%20%20%20%20%20%20%20%20%20can_continue%20=%20true,%20%20%20%20%20%20%20%20%20t;%20%20%20%20%20if%20(s_avail)%20{%20%20%20%20%20%20%20%20%20can_continue%20=%20false;%20%20%20%20%20%20%20%20%20alert('Alreadyopen');%20%20%20%20%20}%20%20%20%20%20setTimeout(function()%20{%20if%20(can_continue)%20{%20create_bookmarklet_iframe(userid);%20}%20},1000);%20})();
Ok, I solved it myself, what I was doing was wrong, instead of creating an iframe I used div, so basically I eliminated iframe, created div by javascript and appended it to document body.
I have numerous iframes that load specific content on my pages. Both the parent and iframe are on the same domain.
I have a scrollbar inside the iframe that doesn't seem to load correctly in all browsers. But when I refresh the iframe it loads perfect. I have no idea why it does this.
I have used the meta refresh, which works but I don't want the page to refresh constantly, just once.
The solution I'm looking for will reload the iFrame content after the iFrame is opened, with a minimal delay.
Edit
I realized that my page loads all of my iframes when the index is loaded. The iframes appear in a jQuery overlay, which is also loaded but visibility:hidden until called. So on this call is when I would want the iframe to be reloaded.
Could anyone help me come up with a Javascript function that reloads the iFrame when I click the link to the iFrame? I've gotten close but I know nothing about js and I keep falling short. I have a function that reloads the page, but I can't figure out how to get it called just once.
I have this so far:
<script type="text/javascript">
var pl;
var change;
pl=1;
function ifr() {
if (pl=1) {
document.location.reload([true]);
alert("Page Reloaded!");
change=1;
return change;
}
change+pl;
}
So basically it uses the document.location.reload which works to reload the page. I'm trying to then make pl change to something other than 1 so the function doesnt run again. I've been calling this JS from the body with onLoad.
All the leads on this went dead, but I did find a code snippet that worked. Not written by me, and I don't remember where it came from. Just posting to help someone should they ever have the same question.
<div class="overlay-content"> //Content container needed for CSS Styling
<div id="Reloader">
//iFrame will be reloaded into this div
</div>
//Script to reload the iframe when the page loads
<script>
function aboutReload() {
$("#Reloader").html('<iframe id="Reloader" height="355px" width="830px" scrolling="no" frameborder="0" src="about.html"></iframe>');
}
</script>
</div>
Basically just loads the iFrame source when the window with the iFrame opens, as opposed to the iFrame loading when the original page loads.
Beyond the scope of the original question, however this jQuery snippit works with cross domain iframe elements where the contentDocument.location.reload(true) method won't due to sandboxing.
//assumes 'this' is the iframe you want to reload.
$(this).replaceWith($(this).clone()); //Force a reload
Basically it replaces the whole iframe element with a copy of itself. We're using it to force resize embedded 3rd party "dumb" widgets like video players that don't notice when their size changes.
On the iframe element itself, set an onload:
iframe.onload = function() {this.contentWindow.location.reload(); this.onload = null;};
(Only works if the iframe's location is in the same domain as the main page)
Here's a complete solution to the original question:
<iframe onload="reloadOnce(this)" src="test2.html"></iframe>
<script>
var iframeLoadCount = 0;
function reloadOnce(iframe) {
iframeLoadCount ++;
if (iframeLoadCount <= 1) {
iframe.contentWindow.location.reload();
console.log("reload()");
}
}
</script>
The updated question is not really clear (what's "the link to the iFrame" and where is it in your snippet?), but you have a few issues with the code:
"calling this JS from the body with onLoad", assuming you mean an iframe's body, means the variable you're hoping to use to avoid infinite reloading will get clobbered along with the rest of the iframe's page when it's reloaded. You need to either load a slightly different URL in the iframe (and check the URL on iframe's onload before reloading) or put the flag variable in the outer page (and access it with parent.variableName - that should work I think)
if (pl=1) { should use ==, as = is always an assignment.
change+pl; has no effect.
According to this answer:
Invoking JavaScript code in an iframe from the parent page
I have an iframe that loads a page that has a div with the id flash_container
<iframe src="http://www.remote.com/a.html" id="iframeID">
I placed this code on my parent page (the page that loads the iframe) but it doesn't seem to work:
document.getElementById('iframeID').contentWindow.targetFunction();
function targetFunction() {
var el = document.getElementById('flash_container');
el.style.zoom = 0.7;
el.style.MozTransform = 'scale(0.7)';
el.style.WebkitTransform = 'scale(0.7)';
}
What I'm trying to do is to zoom-out the inner page inside the iframe from the parent page.
It's impossible to say for certain what's wrong, but I have some ideas you might want to look into.
Make sure that the iframe is loaded. Trying to do something inside a frame that hasn't finished loading clearly won't work
Are you sure that you don't have cross-domain problems. You cannot manipulate the contents of a cross-domain iframe.
Actually you could, if both side (your page and the page in the iframe) agree on sharing information, you could use message passsing. See https://stackoverflow.com/a/7938270/1571709
I have a page on my site which has a blank text field. Next to this is a button which opens up an iframe (using colorbox). This iframe shows a list of files - next to each file is a button which puts the filename in to the text field in the parent window by calling the function below:
function returnImage(file){
$('#input_text_id', window.parent.document).val(file);
parent.$.fn.colorbox.close();
}
This works great, however a problem arises when I click a folder in the iframe. This causes the page in the iframe to load a different page of files. After this, the function no longer works.
Is there anyway that I can use jquery to access the very top parent page and control that? So we would have something like:
function returnImage(file){
$('#input_text_id', theoriginalwindow.document).val(file);
parent.$.fn.colorbox.close();
}
Hope this makes sense, if you require more info, please let me know.
Thanks
In top window:
<script>
function returnImage(file, doc){
$('#input_text_id', doc).val(file);
$.fn.colorbox.close();
}
</script>
From iframe:
<script>
top.returnImage(file, document); // top is top parent window.
</script>
Use top.