HTML Print multiple documents with one dialog - javascript

I'm working on bringing a website up to current standards and removing frames. We have a page for printing multiple documents. When you load the page, it prints all the documents individually, but only asks you to pick your printer once. Here is a simplified version of the code (tested, working)
<!DOCTYPE html>
<html>
<head>
<title>Print test</title>
<script>
function printAll() {
printAllForms.focus();
if (window.print) {
window.print();
}
}
</script>
</head>
<frameset rows="20%,*" id="printAllForms" onload="printAll()">
<frame id="form1" src="page1.html">
<frame id="form2" src="page2.html">
<noframes>You need the frames</noframes>
</frameset>
</html>
I was thinking it might be better to use iFrames. Based on other similar questions, I came up with this:
<!DOCTYPE html>
<html>
<head>
<title>Print test</title>
<script>
function printPage() {
for (var k = 0; k < window.frames.length; k++) {
window.frames[k].focus();
window.frames[k].print();
}
}
</script>
</head>
<body>
<iframe id="form1" src="page1.html"></iframe>
<iframe id="form2" src="page2.html"></iframe>
<button id="btnPrint" onclick="printPage()">Print</button>
</body>
</html>
This works, BUT it shows a dialog every time.
Another method I'm working on is to load DIVs with the data. It will print all as one document. This can be an issue if the user is printing double sided and there is a document with an odd number of pages.
Any help is greatly appreciated. Thank You.

Related

Not able to parameterize the link and give as href to the mentioned frame location

I want to parameterize the href link which so I am passing variable from PHP loop. but when I click on text it doesnot taking exact url and showing url is invalid in the another frame. when i print the parameterized link and stored as link in document.write() but is not taking as href. how can it be done? Thanks in advance
<!DOCTYPE HTML PUBLIC>
<html>
<head>
<script language="javascript" type="text/javascript">
function trial(a,b) {
var link='http://localhost/DashboardAsPerCustomer/BlackLotus_'+a+'_hello.php';
//document.write(link);
parent.table.location.href='link';
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>This is special</title>
</head>
<body>
<p id="demo"></p>
<?php
$names=array("Prajna","Vidya","Deepak","Yajana","Mahadev","Latika","Uzma");
$y=10;
for($x=0;$x<count($names);$x++)
{
echo'<form method="post"><p onClick="trial('.$x.','.$y.');">'.$names[$x].'</p></form><p>';
}
?>
</body>
</html>
Below file is to set 4 different frames and when onclick happen on text which is printing in php loop in above program it calls javascript function with passing parameters and it opens the perticular parameterized link on the 3rd frame.
<!DOCTYPE html>
<html>
<frameset rows="13%,87%,*%">
<frame src="header.php" scrolling=no >
<frameset cols="20%,80%,*%" >
<frame src="phpinsidephp.php" name="tree" >
<frameset rows="40%,60%,*%">
<frame src="http://localhost/JqueryAjax/chart4.php" name="table" scrolling=yes >
<frame src="Tables\project_table.php" name="graph" scrolling=no >
</frameset>
</frameset>
</frameset>
</html>
You must assign variable link not string 'link'
parent.table.location.href=link;

run function on frame when window finish load

I want to run function click on spesific id inside a frame, so i make two html file and i write the script like this
the main html is this index.html
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function autoClick(){
document.getElementById('framing').contentWindow.document.getElementById('trig').click();
}
</script>
</head>
<body onload="autoClick();">
<iframe name="framing" id="framing" src="testing.html" frameborder="0" scrolling="no" height="1200px" width="100%"></iframe>
</body>
</html>
and another one is the testing.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
window.onbeforeunload = function(){
return false; // This will stop the redirecting.
}
</script>
</head>
<body>
<div>
<a id="trig" href='http://www.redirect.com/' target='iName'>Home</a>
</div>
<iframe src="about:blank" name="iName" frameborder="0" scrolling="no" height="1200px" width="100%"></iframe>
</body>
</html>
so when index.html finish load the page, i want the function to click on the <a id=trig" href='http://www.redirect.com/' target='iName'>Home</a>
on testing.html that inside frame framing
and when it clicked, the trig will load a site http://www.redirect.com that will be contained inside iName frame, but the site http://www.redirect.com have a script that will redirect the TLD to the their site, so i put the script for preventing the page to break out the iframe `
window.onbeforeunload = function(){
return false; // This will stop the redirecting.
}
this two html file is hosted on same domain. but when i try to run it, the function click is not triggered, anyone can help me?
Thank you, and sorry for the bad english, it is not my native language

Why do I get "permission denied" in Microsoft Edge when accessing parent iframe

I have some problems calling a function of a specific iframe after reloading another iframe. It works on all major browser but behaves a little weird on Microsoft Edge. You will need the following constellation to get the error. All files are in the same directory on the same server. I haven't set any Content Security Policy.
If you load the Frame1.html everything will be fine and you will get the "alert" message.
But if you click the "Click me" a-tag on the frame4.html, the frame2.html will be reloaded and you will get the "permission denied" error because the parent object (var tmpParent = parent;) is not accessible. If you click the a-tag again it will work without any error.
I think it is a Edge bug, because all other browser can handle it and it only occur on the first click.
The error will also occur if you use top insted of parent.
The code of topFrame.js is used to find the top-most Frame of my site.
I cannot simply use top because it should be possible to embed my site.
Does anybody have a clue?
Thanks a lot!
Frame1.html
<!DOCTYPE html>
<html>
<head>
<title>Frame 1</title>
<script type="text/javascript">
var topFrame = this;
function myAlert() {
alert('alert');
}
</script>
</head>
<body>
<iframe id="overallContentWrapper" name="mainFrame" src="frame2.html" frameborder="0"></iframe>
</body>
</html>
Frame2.html
<!DOCTYPE html>
<html>
<head>
<title>Frame 2</title>
<script src="topFrame.js" type="text/javascript"></script>
<script type="text/javascript">
window.addEventListener("load", function load(event) {
window.removeEventListener("load", load, false);
try {
topFrame.myAlert();
} catch (e) {
alert(e);
}
}, false);
</script>
</head>
<body>
<iframe name="subFrame" src="frame3.html" frameborder="0"></iframe>
</body>
</html>
Frame3.html
<!DOCTYPE html>
<html>
<head>
<title>Frame 3</title>
</head>
<body>
<iframe name="subsubFrame" src="frame4.html" frameborder="0"></iframe>
</body>
</html>
Frame4.html
<!DOCTYPE html>
<html>
<head>
<title>Frame 4</title>
</head>
<body>
Click me
</body>
</html>
topFrame.js
try {
var tmpParent = parent;
var topFrame = tmpParent.topFrame;
while (topFrame === undefined) {
tmpParent = tmpParent.parent;
topFrame = tmpParent.topFrame;
}
} catch (e) {
alert(e);
}
Well i know im putting my neck on the line here trying to guess what IE is trying to tell you but i would assume that using this type of communications is going against what iframes are intended to do.
If you wish to communicate between child frames to parnet frames i suggest using postMessages instead. I think(I did say think) your script is getting blocked against XSS - cross site scripting. So if you wish to communicate some information between parent frame and child frames or the other way around i suggest having a look at postMessges.

Javascript - subscribe to an event of a page from another page

I want to be able to open a popup using window.open and subscribe to page events (onload etc) of the popup in the opener. So i'd want a method in my opener (parent page) to execute when the popup's onload or ready fires. Is this possible using plain js or jquery? Pls don't ask me why i want to do this - this can solve a lot of issues for me.
First page (x.html):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var w = window.open('y.html', 'w');
w.document.getElementById('target').onclick = function () { alert('!'); };
</script>
</body>
</html>
Second page (y.html):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button id="target">target</button>
</body>
</html>
Works for me...

Is it possible to make a link with 'target' attribute run script on another page?

*Update: Ultimately I've decided that accomplishing exactly what I want here isn't possible due to the issues it poses to security. Kalle's answer below gives a solution that is closest to what I want to accomplish.
In order to solve my problem I've created scripts on both pages and will use a sort of push notification that is routed through the server in order for them to communicate.
Thanks for the help!! *
I have two pages. Both windows already exist independently. Page two has a function declared in JS.
I would like to be able to call the function in window two by clicking a link in window one.
Page 1:
<html>
<head>
<title>This is a title!</title>
</head>
<body style="background: lightblue">
Click Me!
</body>
Page 2:
<html>
<head>
<META HTTP-EQUIV="Window-target" CONTENT="my_target" />
<title>This is a title!</title>
<script type=text/javascript>
function clicked() {
alert('test');
}
</script>
</head>
<body style="background: lightblue">
</body>
Since it is on the same domain you can get this to work but would have to change the way you were doing it a little.
First off you would have to open it in a popup using this syntax rather than a new tab:
newwindow=window.open(url,'name','height=200,width=150');
and then you could simply call newwindow.clicked() after the popup is called.
update
just did a quick test and this will open it in a new tab. (sorry its been a while since I used the open function.
newwindow=window.open(url,'name');
Just noticed also that you should wait for the popup to load. So in my Example it would look a little something like this (with jQuery):
var newwindow = window.open('http://www.tylerbiscoe.com/vb/new.html');
$(newwindow).load(function(){
newwindow.clicked();
});
Ok, brand new answer. I hope this is what you were thinking. This is however, when you open page 2 from page 1.. So basically, page 1 would know who page 2 is..
Online example: http://kopli.pri.ee/stackoverflow/6832271.php
Page 1
<html>
<head>
<title>Page 1</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
.ajaxlink {color: blue; cursor: pointer; border-bottom: 1px dotted blue;}
</style>
</head>
<body>
<span id="open_page_2" class="ajaxlink">Open new window</span>
<br>
<br>
Click Me!
<script>
$('#open_page_2').click(function(){
child = window.open('test2.php','page_2','width=600,height=600');
});
$('a[target=my_target]').click(function () {
child.SecondPageFunction();
return false;
});
</script>
</body>
</html>
Page 2
<html>
<head>
<title>Page 2</title>
</head>
<body>
<h1>Your seeing page 2!</h1>
<script>
function SecondPageFunction () {
alert('Second page action got triggered!');
}
</script>
</body>
</html>
The script must be a part of the page you're opening in the new window. You're absolutely correct about it being a security flaw if it was elsewise allowed.
You could add some query string argument that could be picked up onload by javascript in the page you are opening and call your function if the query string arg is present.

Categories