[Zope] Calling external method from javascript

Jonathan Hobbs toolkit at magma.ca
Wed Sep 29 11:08:24 EDT 2004


From: "Laura McCord" <Laura.McCord at doucet-austin.com>
>  I am having trouble with this part of my program. I have an onclick
> function that needs to perform three things. I know that I should have a
> javascript function that basically handles these three operations, but I
> don't know how to call an external method from javascript. I am using a
> zope page template if that helps. I have googled on this topic and there
> is not a lot said about this situation. I did run across someone using
> dtml and calling dtml methods from javascript but did not see any
> examples on using it with zope page templates.
>
> Typically I would call a external function like this: <div
> tal:content="python:here.deleteMAC()"> but this won't work.

AFAIK javascript can not call a zope method (dtml, script or external
method) directly, javascript has to do indirectly by loading a url into the
browser.  You can load into a window, frame, div, etc. Here is an example:

displaywindow.location.href = '<dtml-var
baseurl>DisplayPage?id='+presentorpage+'&stype='+pagetype+'&path='+pagepath

This one line of javascript will load a zope routine called DisplayPage
(with a url path specified by a dtml variable called baseurl) into a browser
window named 'displaywindow'.  It also passes some parameters (id, stype,
path) to the zope routine.

If you need to communicate between the client process (running in the users
browser) and the server process (ie a zope routine) without the user seeing
anything, you can accomplish this using a hidden div in your html (use
javascript to load the url (containing your call to the external method)
into the hidden div), this will allow you to call a zope routine and get
some results without changing the user's browser's display. The zope routine
you call (via the hidden div) can return javascript variables which you can
then parse to determine what you want to do next (embed an 'onload'
javascript command in the html returned by the zope routine to cause a
javascript routine to execute after the hidden div is loaded).

HTH

Jonathan





More information about the Zope mailing list