Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Audit Report Popup</title>
<script>
function openAuditReport() {
const popup = window.open("", "AuditReport", "width=700,height=900");
popup.document.write(`
<html>
<head>
<title>Audit Report</title>
<style>
body { margin: 0; font-family: Arial, sans-serif; }
canvas { display: block; margin: 20px auto; border: 1px solid #ccc; }
</style>
</head>
<body>
<canvas id="auditCanvas" width="650" height="850"></canvas>
<script>
const canvas = document.getElementById('auditCanvas');
const ctx = canvas.getContext('2d');
// Helper function to draw wrapped text
function drawText(ctx, text, x, y, maxWidth, lineHeight) {
const words = text.split(' ');
let line = '';
let testLine = '';
let lineArray = [];
for(let n = 0; n < words.length; n++) {
testLine += words[n] + ' ';
const metrics = ctx.measureText(testLine);
const testWidth = metrics.width;
if(testWidth > maxWidth && n > 0) {
lineArray.push(line);
line = words[n] + ' ';
testLine = words[n] + ' ';
} else {
line += words[n] + ' ';
}
}
lineArray.push(line);
for(let i = 0; i < lineArray.length; i++) {
ctx.fillText(lineArray[i], x, y + (i * lineHeight));
}
return lineArray.length * lineHeight;
}
// Clear canvas
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Title
ctx.fillStyle = '#000';
ctx.font = 'bold 22px Arial';
ctx.textAlign = 'center';
ctx.fillText('AUDIT REPORT', canvas.width / 2, 40);
ctx.font = '16px Arial';
ctx.fillText('For the Year Ended [Date]', canvas.width / 2, 70);
ctx.textAlign = 'left';
let x = 40;
let y = 110;
const maxWidth = 570;
const lineHeight = 22;
// Sections
ctx.font = 'bold 18px Arial';
ctx.fillText('1. Title', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'Independent Auditor’s Report', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('2. Addressee', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'To the Board of Directors and Shareholders of [Company Name]', x, y, maxWidth, lineHeight);
y += drawText(ctx, '[Company Address]', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('3. Introduction', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'We have audited the accompanying financial statements of [Company Name], which comprise the balance sheet as of [Date], and the related statements of income, changes in equity, and cash flows for the year then ended, and a summary of significant accounting policies and other explanatory information.', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('4. Management’s Responsibility for the Financial Statements', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'Management is responsible for the preparation and fair presentation of these financial statements in accordance with [applicable financial reporting framework], and for such internal control as management determines is necessary to enable the preparation of financial statements that are free from material misstatement, whether due to fraud or error.', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('5. Auditor’s Responsibility', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'Our responsibility is to express an opinion on these financial statements based on our audit. We conducted our audit in accordance with [applicable auditing standards]. Those standards require that we comply with ethical requirements and plan and perform the audit to obtain reasonable assurance about whether the financial statements are free from material misstatement.', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('6. Opinion', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'In our opinion, the financial statements present fairly, in all material respects, the financial position of [Company Name] as of [Date], and its financial performance and its cash flows for the year then ended in accordance with [applicable financial reporting framework].', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('7. Signature', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, '[Auditor’s Name]', x, y, maxWidth, lineHeight);
y += drawText(ctx, '[Auditor’s Firm Name]', x, y, maxWidth, lineHeight);
y += drawText(ctx, '[Auditor’s Address]', x, y, maxWidth, lineHeight);
y += drawText(ctx, '[Date]', x, y, maxWidth, lineHeight);
// Footer
ctx.textAlign = 'center';
ctx.font = 'italic 14px Arial';
ctx.fillText('--- End of Report ---', canvas.width / 2, canvas.height - 30);
<\/script>
</body>
</html>
`);
popup.document.close();
}
</script>
</head>
<body>
<button onclick="openAuditReport()">Open Audit Report</button>
</body>
</html>
Your code is almost perfect for opening a popup and drawing an audit report on a <canvas>. However, there is a common issue when injecting <script> tags via document.write() in a popup:
Browsers may not execute the script reliably because the DOM is not always ready when the script runs.
Your <script> block inside the popup is trying to access the canvas immediately after writing the HTML, but the DOM may not be ready yet, so getElementById('auditCanvas') can return null.
Solution:
Wrap your drawing code in a window.onload handler inside the popup's script, so it runs after the DOM is ready.
Replace your <script> in the popup with this:
<script>
window.onload = function() {
const canvas = document.getElementById('auditCanvas');
const ctx = canvas.getContext('2d');
// Helper function to draw wrapped text
function drawText(ctx, text, x, y, maxWidth, lineHeight) {
const words = text.split(' ');
let line = '';
let testLine = '';
let lineArray = [];
for(let n = 0; n < words.length; n++) {
testLine += words[n] + ' ';
const metrics = ctx.measureText(testLine);
const testWidth = metrics.width;
if(testWidth > maxWidth && n > 0) {
lineArray.push(line);
line = words[n] + ' ';
testLine = words[n] + ' ';
} else {
line += words[n] + ' ';
}
}
lineArray.push(line);
for(let i = 0; i < lineArray.length; i++) {
ctx.fillText(lineArray[i], x, y + (i * lineHeight));
}
return lineArray.length * lineHeight;
}
// Clear canvas
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Title
ctx.fillStyle = '#000';
ctx.font = 'bold 22px Arial';
ctx.textAlign = 'center';
ctx.fillText('AUDIT REPORT', canvas.width / 2, 40);
ctx.font = '16px Arial';
ctx.fillText('For the Year Ended [Date]', canvas.width / 2, 70);
ctx.textAlign = 'left';
let x = 40;
let y = 110;
const maxWidth = 570;
const lineHeight = 22;
// Sections
ctx.font = 'bold 18px Arial';
ctx.fillText('1. Title', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'Independent Auditor’s Report', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('2. Addressee', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'To the Board of Directors and Shareholders of [Company Name]', x, y, maxWidth, lineHeight);
y += drawText(ctx, '[Company Address]', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('3. Introduction', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'We have audited the accompanying financial statements of [Company Name], which comprise the balance sheet as of [Date], and the related statements of income, changes in equity, and cash flows for the year then ended, and a summary of significant accounting policies and other explanatory information.', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('4. Management’s Responsibility for the Financial Statements', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'Management is responsible for the preparation and fair presentation of these financial statements in accordance with [applicable financial reporting framework], and for such internal control as management determines is necessary to enable the preparation of financial statements that are free from material misstatement, whether due to fraud or error.', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('5. Auditor’s Responsibility', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'Our responsibility is to express an opinion on these financial statements based on our audit. We conducted our audit in accordance with [applicable auditing standards]. Those standards require that we comply with ethical requirements and plan and perform the audit to obtain reasonable assurance about whether the financial statements are free from material misstatement.', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('6. Opinion', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, 'In our opinion, the financial statements present fairly, in all material respects, the financial position of [Company Name] as of [Date], and its financial performance and its cash flows for the year then ended in accordance with [applicable financial reporting framework].', x, y, maxWidth, lineHeight);
y += 20;
ctx.font = 'bold 18px Arial';
ctx.fillText('7. Signature', x, y);
y += 30;
ctx.font = '16px Arial';
y += drawText(ctx, '[Auditor’s Name]', x, y, maxWidth, lineHeight);
y += drawText(ctx, '[Auditor’s Firm Name]', x, y, maxWidth, lineHeight);
y += drawText(ctx, '[Auditor’s Address]', x, y, maxWidth, lineHeight);
y += drawText(ctx, '[Date]', x, y, maxWidth, lineHeight);
// Footer
ctx.textAlign = 'center';
ctx.font = 'italic 14px Arial';
ctx.fillText('--- End of Report ---', canvas.width / 2, canvas.height - 30);
};
<\/script>
Replace your popup.document.write(\ ... `);with this (note thewindow.onload`):
popup.document.write(`
<html>
<head>
<title>Audit Report</title>
<style>
body { margin: 0; font-family: Arial, sans-serif; }
canvas { display: block; margin: 20px auto; border: 1px solid #ccc; }
</style>
</head>
<body>
<canvas id="auditCanvas" width="650" height="850"></canvas>
<script>
window.onload = function() {
// ... (drawing code here, as above)
};
<\/script>
</body>
</html>
`);
window.onload (or DOMContentLoaded) when injecting scripts that access DOM elements via document.write().Let me know if you want a full copy-paste version of the corrected code!