We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Retrieving Arguments
Page Index Toggle Pages: 1
Retrieving Arguments? (Read 521 times)
Retrieving Arguments?
Jun 2nd, 2009, 12:50am
 
Nice language, nice tool.

I'm going to embed a generated  applet to a web page and I need to transfer parameters to the processing-code. I couldn't find any hint how to read the args of Base.main(args). I'd expect it to be a global variable or something like that?
Re: Retrieving Arguments?
Reply #1 - Jun 2nd, 2009, 1:28am
 
A PApplet is a classical Java applet, so you can use getParameter("PARAMETER_NAME"); to access the HTML-defined parameters.
Re: Retrieving Arguments?
Reply #2 - Jun 5th, 2009, 4:38am
 
This requires editing the index.html file which calls the applet.

Is there a way of passing the parameters from the url to the applet
index.html?name="marc"
Re: Retrieving Arguments?
Reply #3 - Jun 5th, 2009, 6:03am
 
No, AFAIK, these parameters are meaningless for pure HTML files.
Actually, you can use them... But it needs to add some JavaScript code which will take the URL and process these parameters.

...

Although I think the Java applet can do the same too! So it won't use the getParameter() method, but the getDocumentBase() one (untested!).
Re: Retrieving Arguments?
Reply #4 - Jun 5th, 2009, 7:49am
 
PhiLho  wrote on Jun 2nd, 2009, 1:28am:
you can use getParameter("PARAMETER_NAME");
Just noticed: Processing provides the param() method for this!
Re: Retrieving Arguments?
Reply #5 - Jun 6th, 2009, 10:22am
 
I don't know about getDocumentBase(), but here's some JavaScript that does the trick:

Code:
<script type="text/javascript" charset="utf-8">
   var pgArgs = new Object();
   function getPageArgs() {
       var argElems = window.location.search.substring(1).split("&");
       for (i=0; i < argElems.length; i++) {
           var elem = argElems[i].split("=");
           pgArgs[elem[0]] = unescape(elem[1]);
       }
   }
   getPageArgs()
</script>


(I probably pinched this code from somewhere, but I don't have any notes about its origin.  If anyone reading this is the original author, please let me know and I'll be sure to credit you.)

Insert the above sometime before your page finishes loading and before you need the parameters.  I simply insert it near the top of the HTML's <head> section. Then, where you need to pass your parameter to the applet, insert:

Code:
<script type="text/javascript" charset="utf-8">
   document.write('<param name="paramID" value="' + pgArgs["urlParamID"] + '">');
</script>


(or the equivalent syntax for the applet embedding method you choose). This will allow you to use a URL of the form:

 http://www.example.com/processingsketch.html?urlParamID=value

and pass the parameter's "value" to your sketch.

Does that make sense?  This seems to work pretty well on the few browsers where I've tested.  Can anyone with more JavaScript experience suggest a better way?
Page Index Toggle Pages: 1