<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
Related
I'm using PhantomJS to retrieve the source code of a website after some AJAX manipulations of the DOM. Then using jQuery, I'm simulating a click on a button element (which has it's link hidden in an obscure JavaScript script). This button's onClick effect is to open a new window.
What I'd like to do is retrieve the source code of this new window.
What I cannot do is use the window.open("url") method in JS because I cannot retrieve the url of the new window since it's obfuscated in a script.
Some workaround I've been looking at but haven't succeeded to implement:
Return the new window in any way with the click() function.
Switch the focus to the new window in JS.
Retrieve the url of the button's onClick whitout having to parse the JS script.
Here's what I've got at the moment:
var page = require("webpage").create();
page.open("http://www.example.com", function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
var page = page.evaluate(function() {
$("#myButton").click(function() {
// ???
});
});
phantom.exit();
});
});
I'm open to other frameworks/technologies if there is a solution!
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 was developing a struts web application, in one of my JSP I created a button to open a new window. The way I did it is:
window.open(location,'_blank');
everything works fine except the new window is always scrolled down to the bottom itself. How to show the top part instead?
I am using Chrome.
Include place this JS at the bottom of the page just before the closing body tag.
<script>window.scrollTo(0,0);</script>
Or you could simply use jQuery if your page is already including it:
<script>$(document).ready(function(){$(window).scrollTop(0);});</script>
Try something like this:
var newWindow = window.open(someUrl, '_blank');
newWindow.onload = function(){
newWindow.scrollTo(0, 0);
}
newWindow.onload();
Try this
window.open(location,'_blank','top=50');
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.
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);
});