You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Mark <ma...@xeric.net> on 2011/09/06 04:04:35 UTC

javaScriptSupport.addScript vs script in .tml file

I am trying to get the call back from Google's +1 button to work
correctly. What I'm finding is that if I put the script directly into
the .tml file it works fine. If I try to add the script using
addScript. For example, if I put this in the .tml file:

<script type="text/javascript">
	function plusoneCalled(plusone) {
		window.alert('+1 Triggered');
	}
</script>

It works as expected.  However if I do this in the .java file:

    @BeginRender
    void beginRender(MarkupWriter writer) {
        javaScriptSupport.addScript("function plusoneCalled(plusone) {
window.alert('+1 Triggered');}");
    }

The javascript that gets rendered out to the browser is:

<script type="text/javascript">Tapestry.onDOMLoaded(function() {
	function plusoneCalled(plusone) { window.alert('+1 Triggered');}
});
</script>

This code gives me an error in Chrome Developer Tools of:

Unsafe JavaScript attempt to access frame with URL
http://localhost:8080/tap5-plusone/JSFromJava from frame with URL
https://plusone.google.com/_/+1/confirm?hl=en-US&url=http%3A%2F%2Fwww.google.com%2F&su=1&referer=http%3A%2F%2Flocalhost%3A8080%2Ftap5-plusone%2FJSFromJava&jsh=r%3Bgc%2F23579912-2b1b2e17#id=I4_1315273724285&parent=http%3A%2F%2Flocalhost%3A8080&rpctoken=642223491&_methods=mouseEvent%2C_onopen%2C_ready%2C_onclose%2C_close%2C_open%2C_resizeMe.
Domains, protocols and ports must match.

Firefox doesn't seem to show me an error, but it doesn't trigger the alert.

If it didn't work when I embedded the script directly in the .tml I
would understand, but since it works there, I don't understand why it
doesn't work when I add it using addScript.

Any suggestions?

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: javaScriptSupport.addScript vs script in .tml file

Posted by LLTYK <LL...@mailinator.com>.
If you declare a function (your plusOneCalled) inside another function
(Tapestry.onDOMLoaded), it won't be visible outside the function.

Don't use addScript, unless you can initialize the button itself in
addScript as well. Or you can use addScript and add the function to the
window variable ( window.plusOneCalled = function () { alert('...'); } ),
which should make it visible.

You could also put plusOneCalled function in its own js file.

--
View this message in context: http://tapestry-users.832.n2.nabble.com/javaScriptSupport-addScript-vs-script-in-tml-file-tp6762486p6763598.html
Sent from the Tapestry Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org