Dynamically Inject Resources during a Partial Refresh


<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.ibm.com/xsp/core xsdxp://localhost/xsp~core.xsd">
	
	<xp:this.beforePageLoad>
		<![CDATA[#{javascript:viewScope.doMyCustomFunc = false}]]>
	</xp:this.beforePageLoad>
	
	<xp:button value="Inject..." id="button1">
		<xp:eventHandler event="onclick" submit="true"
			refreshMode="partial" refreshId="injectionTarget">
			<xp:this.action>
				<![CDATA[#{javascript:
viewScope.doMyCustomFunc = true;

var myCustomScript = "<script type=\"text/javascript\">function myCustomFunc(msg){ XSP.alert(msg) }</script>\n";

var someOtherScript = "<script type=\"text/javascript\">dojo.require('dojo.parser')</script>\n";
someOtherScript += "<script type=\"text/javascript\">dojo.config.parseOnLoad = true</script>\n";
someOtherScript += "<script type=\"text/javascript\">dojo.require('dijit.Calendar')</script>\n";

var w = facesContext.getExternalContext().getResponse().getWriter();
w.write(myCustomScript);
w.write(someOtherScript);
				}]]>
			</xp:this.action>
		</xp:eventHandler>
	</xp:button>
	
	<xp:div id="injectionTarget">
		<xp:div rendered="#{viewScope.doMyCustomFunc}">
			<script type="text/javascript">myCustomFunc("hello world!")</script>
		</xp:div>
	</xp:div>
</xp:view>
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...