Forum How do I...?

Javascript problems

mikedfunk
First of all there is hardly any documentation to be found about how the javascript engine works. It took me quite a while to realize the --javascript option is not included in the official PHP wrapper. I added it in there myself and confirmed in the logs that it is running with javascript enabled now:
Mon Sep 19 16:14:37 2011: ---- begin
Mon Sep 19 16:14:38 2011: Loading document...
Mon Sep 19 16:14:38 2011: Running scripts...
Mon Sep 19 16:14:38 2011: Converting document...
Mon Sep 19 16:14:38 2011: Resolving cross-references...
Mon Sep 19 16:14:38 2011: finished: success
Mon Sep 19 16:14:38 2011: ---- end

I've tried linking the script file with this in the head of the html document:
<script src="/path/to/file.js" type="text/javascript"></script>

with some simple javascript:
element = document.getElementById("the_body");
element.style.color = "red";

and it doesn't work. Am I doing something wrong? I also tried to run the code without the src link in the html file directly such as:
<script type="text/javascript">
// same javascript code as above...
</script>

to no avail. How the heck do I get javascript to run??
mikeday
Prince 8.0 doesn't support the DOM "style" property, but we're rushing to add this in Prince 8.1. You can set the style attribute using the setAttribute method, however.
mikedfunk
Ok. Is there any documentation on what code you do support?? I don't get any error messages and there is no documentation I can find so I am feeling around in the dark here.
mikeday
Sorry about that, the nature of JavaScript is that unknown properties silently return "undefined", although you should get error messages for syntax errors or attempts to write to undefined values or call undefined functions.

We have added some new JavaScript documentation that details Prince-specific JavaScript interfaces, and lists some of the DOM features that we don't yet support.
mikedfunk
Thanks Mike, this is just what I was looking for! We're planning on buying a server license as soon as our app is through testing with Prince, which may be this week.

Here's a related question:

I am trying to make an even total number of pages, so add one page if it's an odd total. I tried various methods of page-break-after: right with no success. According to this thread page-break-after only supports avoid and always. It does say in the documentation that left and right are available, though.

Since I couldn't get this to work, I tried an extra div with page-break-before: right; instead. This added one page on odd total and two pages on even total. I mentioned this to Jim from Wycliffe and he said that Javascript could conditionally remove that div if there were an even total number of pages. I'm looking at the new documentation here and it says that "The Prince.pageCount property can be accessed after document conversion has finished." I set this javascript to confirm:
function add_odd()
{
	if (Prince.pageCount % 2 == 1)
	{
		Log.data('odd total', Prince.pageCount);
		var extra = document.createElement('div');
		extra.setAttribute('id', 'extra_page');
		document.body.appendChild(extra);
	}	
}
Prince.addEventListener("complete", add_odd, false);

It logged the data but did not add the div. I expect that's because the document is already complete at this point. Is there any way you know of to add one page to the end if odd total number of pages and zero if even, through javascript or otherwise?
mikeday
That's a very good attempt, but yes we only run this after conversion is finished, and modifying the document will not trigger reconversion at this time.

Investigation of "page-break-after: left | right" has shown that it should actually work as expected for elements that are followed by other content. The only time it doesn't work is exactly when you need it: for the very last page in the document. Here is an example:
<html>
<body>
<div style="page-break-after: right">
<h1>Hello, world!</h1>
</div>
<div style="page-break-after: right">
<h1>Hello, world!</h1>
</div>
<div style="page-break-after: right">
<h1>Hello, world!</h1>
</div>
</body>
</html>

This document will result in a PDF with five pages. Prince is not bothering to add a blank page at the end, as there is no content to follow it. We are going to change this behaviour in Prince 8.1 so that it does add a blank page even at the end, which will make this property behave consistently and allow it to be used in the way that you need.
mikeday
Prince 8.1 is now available for download, with support for the DOM style property in JavaScript, and we have changed page-break-after so that it can be used to add a blank page at the end of the document and guarantee an even/odd page count.