10 Replies - 3323 Views - Last Post: 18 November 2015 - 08:38 AM Rate Topic: -----

#1 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 13-November 15

Javascript code will not execute if a cfdocument tag is used

Posted 13 November 2015 - 07:34 AM

I have a popup form action page that opens by using the form/target="_blank" attribute.


The new document does a database insert and then creates a PDF using the <cfdocument> tag.


I'm trying update some elements on the parent/opener form page as it is common for the client to continue using the form, make some changes, and generate another PDF. If I comment out the <cfdocument> tag my code works fine, the parent document is updated as I expected.


I'm not sure how to proceed, help much appreciated!


thanks, Lyndon


Javascript I'm trying to run just before the <cfdocument> tag:

<script>

        window.opener.document.getElementById('photoListTitle').innerHTML='test update';

</script>



Tag Attributes:
<cfdocument

        localUrl="yes"

        format="PDF"

        pageType="custom"

        pageWidth="2.2"

        pageHeight="3.375"

        marginBottom="0"

        marginTop="0"

        marginLeft="0"

        marginRight="0"

        orientation="portrait"

        backgroundVisible="yes"

        fontEmbed ="true"

        filename="PDFs/#Replace(BadgPhotoName,'.jpg','.pdf','All')#"

        overwrite="true">

          some simple html...

</cfdocument>



Lyndon

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript code will not execute if a cfdocument tag is used

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,609
  • Joined: 13-January 08

Re: Javascript code will not execute if a cfdocument tag is used

Posted 13 November 2015 - 09:28 AM

Welcome to DIC Lyndon!

So, in general terms, what CFDOCUMENT does is allow you to put into a PDF, output that is generated by ColdFusion. So, conceptually, a PDF is being created whenever you run CF code that contains that tag.

Now, your question is why isn't your Javascript, that sits outside the code block that is contained within the CFDOCUMENT tags not working. Think on that a moment. Javascript can work with anything in it's own and select other DOMs (document object models). In less technical terms, if it's on a web page, it can work with elements on that same web page. But, when you use the CFDOCUMENT tag, the generated HTML content isn't in that web page anymore...it's in a separate PDF document.

Essentially, your Javascript that precedes the CFDOCUMENT block can't see the HTML generated content within that block because that block doesn't exist in a DOM that your Javascript snippet can see. It exists in a PDF now.

You can include Javascript within the CFDOCUMENT block and most times it will function appropriately within the generated PDF (heavy emphasis on "most times"). Your JS snippet is trying to alter the InnerHTML of an element inside the PDF from outside the PDF. You can put it inside the PDF generated block and as long as the triggering event is something that can be done in a PDF then you may still be in luck. This is more of a business logic question though at that point.

Hope that helps. Good luck and post back here if you have any further questions!
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 13-November 15

Re: Javascript code will not execute if a cfdocument tag is used

Posted 13 November 2015 - 10:38 AM

I tried putting the JS in the cfdocument code block and no happiness. >_< >_<:(

This my not be possible???

Thanks so much Craig
Was This Post Helpful? 0
  • +
  • -

#4 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,609
  • Joined: 13-January 08

Re: Javascript code will not execute if a cfdocument tag is used

Posted 13 November 2015 - 10:43 AM

Right. Again, the "window.opener" bit is referring to the window that opened the current window. Inside a PDF, since the document is outside the DOM, there is no parent window to refer to.
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 13-November 15

Re: Javascript code will not execute if a cfdocument tag is used

Posted 13 November 2015 - 11:11 AM

After reading your comments, I have an idea. Currently I'm using cfswitch to cfinclude the selected cfdocument (client has 12 different print templates to choose from).

I'm going to try cflocation with a url variable, instead of cfinclude, after my data insert JS code to get the cfdocument they select.

will report results...
Was This Post Helpful? 0
  • +
  • -

#6 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 13-November 15

Re: Javascript code will not execute if a cfdocument tag is used

Posted 16 November 2015 - 07:59 AM

Well.... Disappointment.



<cflocation> has the same behavior??? If I comment out the cflocation tag the parent page updates, execute it and it does not.



I thought for sure it would work since I'm opening a new template.



Any ideas?
Was This Post Helpful? 0
  • +
  • -

#7 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 13-November 15

Re: Javascript code will not execute if a cfdocument tag is used

Posted 16 November 2015 - 08:39 AM

Happiness!

An <Iframe> tag did it.

Hope this helps others.

Thanks for the help.

FYI, Passing the BadgeID as a url variable did not work use the scr attribute, but a session var works just fine.

<cfoutput>

    <script>

        window.opener.document.getElementById('photoTitle').innerHTML='#BadgeID#';

    </script>

</cfoutput>

<cfset Session.BadgeID=#BadgeID#>

<cfswitch expression="#Form.CardFormat#">

    <cfcase value="White">

        <iframe src="WhiteOccupationalLicense.cfm"></iframe>

    </cfcase>



Was This Post Helpful? 1
  • +
  • -

#8 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,609
  • Joined: 13-January 08

Re: Javascript code will not execute if a cfdocument tag is used

Posted 16 November 2015 - 10:06 AM

Good for you, man! Thanks for posting back with your solution.
Was This Post Helpful? 0
  • +
  • -

#9 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 13-November 15

Re: Javascript code will not execute if a cfdocument tag is used

Posted 16 November 2015 - 11:17 AM

The only downside is the user gets stuck with the no-content window the iframe is in and must close it. But the benefit to the complex data I've populated the parent window with is more that worth that small irritation.
Was This Post Helpful? 0
  • +
  • -

#10 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 13-November 15

Re: Javascript code will not execute if a cfdocument tag is used

Posted 18 November 2015 - 08:34 AM

More win :bananaman:/>/>/> :bananaman:/>/>/>

I didn't like the window that does the database work, updates the parent window, and launches the iframe window to be left open. It's very irritating for the client. So I played with all of the iframe event triggers and found that onload would do the job.

Hope this helps someone...
<cfswitch expression="#Form.CardFormat#">
	<cfcase value="White">
		<iframe src="WhiteOccupationalLicense.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Green">
		<iframe src="GreenOccupationalLicense.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Red RP">
		<iframe src="RPGamingBadge.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Red WRD">
		<iframe src="WRDGamingBadge.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Red All Tracks">
		<iframe src="AllTracksGamingBadge.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Under 16">
		<iframe src="Under16.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Under 18">
		<iframe src="Under18.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Green Under 18">
		<iframe src="GreenUnder18.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Spouse">
		<iframe src="SpousePass.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="FMT Green">
		<iframe src="FairMeetOnlyGreen.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="FMT White">
		<iframe src="FairMeetOnlyWhite.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfcase value="Blue Staff">
		<iframe src="OHRCStaffBadge.cfm" onload="top.window.close();"></iframe>
	</cfcase>
	<cfdefaultcase>
		<div style="color: Blue; font-family: Calibri; font-weight: bold; font-size: 24px; text-align: center;">
	        <br /> <br />
			You didn't choose a card type<br />
			Please choose a Card Template and try your print job agian
			 <br /> <br />
			 <div style="color: red; font-family: Calibri; font-weight: bold; font-size: 24px; text-align: center;">
			 	You may close this window
			 </div>
		</div>
    </cfdefaultcase> 
</cfswitch>


Was This Post Helpful? 1
  • +
  • -

#11 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,609
  • Joined: 13-January 08

Re: Javascript code will not execute if a cfdocument tag is used

Posted 18 November 2015 - 08:38 AM

Very nice! Thanks for sharing!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1