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.
Related
I have html page SomePage with onload event:
<body onload="someEvent()">
I attached to the SomePage js file with someEvent function:
function someEvent()
{
someFunction();
}
When I open SomePage, the someEvent function is launching in first tab. But it not launch when I open SomePage in new tab. How relaunch js function in every new opened tab or window of the same page?
Update:
When I run somePage in Visual Studio witn JavaScript debugging mode and put breakpoint in someEvent function, Debugger breaking it only in first opened tab, when I open second tab, Debugger do not break the point. Therefore, I decided, that my function not refreshed in second tab. After your answers I realized that it's not JavaScript problem and my previous example work correct only without breaking point in new opened tab. Thank you for your help.
It should work in every tab as long as long as you are loading the same page.
You can test by making an alert call in the onLoad callback function.
<body onload="someEvent()">
<script>
function someEvent()
{
alert('hi');
}
</script>
We may need more info on this. If you are just going to SomePage on a separate tab then anything that happened in the first tab should happen in the second. If that isn't what you are doing then the next tab is being opened by the first and that is where we need more info.
If I need to do something on load I usually set the following up in my javascript file and link it to my html page through a <script src="/path/to/file">
window.addEventListener("load", () => {
runFunctionAfterLoaf();
});
Try using
window.onload = function() {
someFunction();
};
From my webpage, I am opening a new page in different tab. When the page in different tab will be loaded, I want to close my webpage i.e. Suppose I am on pageA and I opened pageB using window.open(). Now, when pageB will be opened, I want to close pageA. I tried this jsFiddle -
function onClickBtn()
{
var win = window.open('http://www.google.com','_blank','');
setTimeout(function () {
win.close();
}, 5000);
};
This is my HTML markup -
<input id="btn1" type="button" value="Click me" onclick="onClickBtn()"/>
However, this code is closing pageB ,not PageA.
I have tried a jsFiddle https://jsfiddle.net/gdso9eeg/
Please suggest me a suitable solution.
It is doing what it should do.
You opened the new window with that variable and on using close() with it, it is closing the window which it opened.
If you want to close the parent then open the new page on the parent.
Try this:
In place of _blank put _parent
If page is redirecting on the same page window then window.close will not work due to some browser security for this scenario type we need to use one of the following way.
1.window.history.back();
2.document.referrer
3.Request.UrlReferrer server side
The .close() should follow Javascript's window, not win.
Try
window.close();
Good luck!
EDIT:
Have you checked this post window.close and self.close do not close the window in Chrome ?
"..javascript must not be allowed to close a window that was not opened by that same javascript."
It also offers some workarounds to the issue.
Trying to open a link in a framed page and close the child window. The link bellow is in a child window and when I click it opens the link in the framed page, but did not close the child window
<a target="Resultado" href="?Tela=1"
onClick="javascript:return confirm('TryMe');window.close();">
I have used a code like this to close the window... but couldn't get it to work with the above code.
<a href="javascript:window.opener='Resultado';window.close();">
Try this:
<a target="Resultado" href="?Tela=1" onclick="clickHandler(event, this);">Link</a>
And declare this:
function clickHandler(e, el) {
var choice = confirm('TryMe');
if (!choice) {
e.preventDefault();
}
window.close();
}
I wasn't sure of your original use of window.close() since it came after the return and would never execute, so it's up to you to move it to where you want.
Create another webpage on your webserver & use that as the Custom URL thankyou page for your form.
In that new webpage have just one line of code.
<body onload="javascript:window.opener.childClosed();window.close();">
--
That code will call a javascript function in the parent window and then close the child popup; you can then use that javascript function to do a redirect on your parent page.
I am not to sure where the URL of your 'Thank you for requesting...' page is, but here is some javascript to redirect to google on your parent page after the form is submitted.
function childClosed() {
window.location = "http://www.google.com/"
}
You can put that script before your closing <body> tag.
--
Hopefully I have understood your query OK, let me know if you have any questions or need any clarification on this possible solution.
-
I have made a very basic clone of your webpage & form here if you want to test out the functionality, 'Factoring Question?' is the link that contains this code.
the link
<script type="text/javascript">
goog_report_conversion_website = function() {
$('#converstion_tracker_iframe').attr('src', '/conversion_tracker/website/');
}
</script>
The expected behaviour of above link should be open up the link in a new window and execute the 2 js functions. However, the goog_report_conversion_website() function is causing the parent page to reload while opening up a new window.
I suspect this is because I am changing the DOM of the parent page, could anyone please confirm with me? Also how to stop the parent page reloading on clicking the link?
EDIT:
looks like change the src of an iframe will cause the page to reload
Did you try adding return false like this?
the link
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 */ });