loading site in iframe has error in IE - javascript

I have a url that I want to load in an iframe that's inside a pop-up window. The parent window has a script that inserts a script to the pop-up window so that it the popup will refresh the parent window when the pop-up is closed.
function popUp(url) {
newwindow = window.open('', 'mypopup', '');
var tmp = newwindow.document;
tmp.write('<!DOCTYPE html><html><head><title>My Pop-Up</title></head>');
tmp.write('<body><iframe src="' + url + '" scrolling="no" frameborder="0"></iframe>');
tmp.write('<script type="text/javascript">window.onunload = function(){ window.opener.location.reload(); }</script></body></html>');
tmp.close();
}
I chose to write the html via javascript because I can't edit the page being loaded (I need to really reload the parent when the pop-up is closed.) This works on chrome and firefox but IE has an error 'SCRIPT5: Access is denied.' I'm not trying to access any part of the page that is being loaded in the iframe, I just want to load it.
When I open the page in a new IE tab, it loads fine. Also, if I load the url via showModalDialog(), the page loads. I wish I could just use showModalDialog() but when the save button is clicked on the pop-up page, another pop-up opens so I guess that's out of the question.
Help would be greatly appreciated.
Thank you :)

Well, if you want to check only when the pop up is closed then you can initiate external timer to check this:
var checkPopup = window.setInterval(function() {
if (newwindow.closed) {
window.clearInterval(checkPopup);
document.location.reload();
}
}, 100);
This way you don't rely on fancy scripting inside the pop up itself.

Related

To open the already opened tab using window.open() function without reloading the already opened tab

I was writing OnClick javascript code to open the new web page.
Expectation:
I want my to open the new page if that page is not already openend or open the already existing page without reload if that page is already opened.
My code:
window.open(url,'dialers').focus();
This code always reloads the child page if already opened. Please help to achieve my ask.
You can store the window object into a variable and call the focus() method multiple times like this:
let newWindow = window.open(url,'dialers')
newWindow.focus()
doThing()
newWindow.focus()
doAnotherThing()
newWindow.focus()
HTML5 introduced the history.pushState(page, caption, replace_url) that should not reload your page. OR if you want to modify while page load you can do this.
var currentURL = www.getitsol.com;
window.location.replace(currentURL + "?data=" + data.result, "_self");

How to refresh another page using javascript without opening the same page in a new tab

Is it possible to refresh a page from another page using Javascript or JQuery without opening the same page in a new tab.
JS:
var newtab = window.open('http://localhost:8081/app/home');
newtab.document.location.reload(true);
I tried the above, but here, it will open a new tab, with the same page, which is already opened in the browser.
Please suggest a method.
I got the idea from a previous Question , here they used window Object Reference to reload the popup window, but for me it wont work, because, the parent window and child window runs in 2 different ports. So using the same trick, what i did is :
HTML:
<a onclick="openNewTab()">app2</a>
<a onclick="refreshExistingTab()">Refresh</a>
JS:
<script>
var childWindow = "";
var newTabUrl="http://localhost:8081/app/home";
function openNewTab(){
childWindow = window.open(newTabUrl);
}
function refreshExistingTab(){
childWindow.location.href=newTabUrl;
}
</script>
refreshExistingTab() this instend of refreshExistingTab
take a look at https://developer.mozilla.org/en-US/docs/Web/API/Window.open
basically if you do window.open and specify a window name it will overwrite that window with the url you provided.
so if you open the page each time with same window name, it should overwrite it each time you do it again from that other page.

I need to reload current location js

I need to reload current location(href) via JS :
I have a button that open a new window and when that window closed I need to reload the location in window that was initiator(parent). It is work in my browser, but one said me that his window does not reload( he has last version chrome and mac book)
I have the followin code:
var loginWindow = window.open(...);
loginWindow.focus();
loginWindow.onbeforeunload = function() {
location.reload(); //when the popUp window close
//I even tried this:
window.location.href = window.location.href;
}
but the page not reloaded
AS onbeforeunload does not work here it is recommended to check if the window exists with certain interval Please find the reference link below
Check this post

Printing a web page using just url and without opening new window?

<html>
<head>
<script type="text/javascript">
var URL = "http://localhost:8000/foobar/";
var W = window.open(URL); **Note1**
W.window.print();
</script>
</head>
<p> Print ME...............</p>
</html>
I am using this script to print a webpage.
My views render this page and The JS take care all other things.
But I dont want to open new window for that. So, What should I use instead of window.open(URL) so no new window opens. Similarly, I don't want to open new window for print function.So, Whenever I render this page it do all stuff on the same page. No new window, No new tab. How can I achieve this. I google but nothing seems working.
You can do this using a hidden iFrame (I'm using jquery for the example):
function loadOtherPage() {
$("<iframe>") // create a new iframe element
.hide() // make it invisible
.attr("src", "/url/to/page/to/print") // point the iframe to the page you want to print
.appendTo("body"); // add iframe to the DOM to cause it to load the page
}
This will load the page you want to print. To print, you can add javascript code to the print page so that it gets printed after loading:
$(document).ready(function () {
window.print();
});
This will print the page without showing a new window. I've tested this in IE8,9 and Google Chrome, so I'm not sure if this works for Safari or Firefox, though.
There's a nice example on MDN how to do that with a hidden iframe https://developer.mozilla.org/en-US/docs/Printing#Print_an_external_page_without_opening_it
In reference to #andragon's answer. updated on top of it.
You can do this using an iFrame(Not hidden because hidden iFrame prints the blank page in latest versions of browsers. You can hide after the print is triggered)
function loadOtherPage(link) {
$("<iframe class='printpage'>") // create a new iframe element
.attr("src", link) // point the iframe to the page link you want to print
.appendTo("body");
}
This will load the page link you want to print.
On loading the print page link you can call javascript.
$(document).ready(function () {
window.print();
});
window.onafterprint = function () {
$('.printpage', window.parent.document).hide();
}
This will print the page from the same window and onafterprint Event is triggered when a page has started printing, or if the print dialog box has been closed
window.parent.document is to hide the iFrame block on the parent page.
I'm using Asp .net core with razor html as view, in this case I have used window.print() to print the page then used window.onafterprint to back to the page where used want to be redirected.
You can use ViewBag to replace the "/NewSales" URL.
NOTE: window.onafterprint will be called whenever user clicks Cancel/Submit/Print button in that pop-up.
$(document).ready(function () {
window.print();
window.onafterprint = function () {
window.location.href = "/NewSales";
}
});
function CallPrint() {
var prtContent = document.getElementById('main');
var WinPrint = window.open('', '', 'width=800,height=650,scrollbars=1,menuBar=1');
var str = prtContent.innerHTML;
WinPrint.document.write(str);
WinPrint.document.close();
WinPrint.focus();
}
Call this javascript function on Print button click."main" is the id of the div which we have to print without opening into new window.I want to notify that this will print the current page div.
Try and rever in case of any issue.
Thanks,
Gourav

Detaching/opening an iframe into a new window

I have an iframe setup within a page and basically want to know whether it's possible to have a button in this iframe and when pressed, opens the iframe into a new browser window, showing the contents of the iframe.
I am planning on using either JavaScript or jQuery to achieve this. I am using IE6.
$('.button').click( function(){
window.open($('iframe').attr('src'),'mywindow','width=400,height=200');
});
For what you need (to open the same page where this button), no matter if it's an iframe or if in the home page.
The only difference is if you want that data to open in new window, are a reflection of the same page, such as data that can be an input.
If you care about who are the same data:
$("#mybuttonOpenWin").click(function(){
window.open(window.location.href);
});
If you are interested, you can try this code:
$("#mybuttonOpenWin").click(function(){
var mref = window.open(window.location.href);
(function = onReadyRef(xref){
if(xref.window.document.readyState=="complete"){
$(xref.window.document).find("body").html($("body").html());
}
else{
onReadyRef.call(this, xref);
}
})(mref);
});

Categories