For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > November 2004 > Variable interpolation inside an externa js file?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Variable interpolation inside an externa js file?
Siegfried Heintze

2004-11-22, 8:55 pm

In my perl script I have put my javascript code in a separate file with the
extension of js. I reference it with a javascript tag in the HTML. I have
some code in there that is not in a procedure: it just executes prior to the
body of the page loading as inline statements.

Now I discover I need some of those URLs in my external javascript file to
change depending on some perl parameters.

This is not a procedure so I cannot just change some javascript procedure
parameters. Perl does not process this file so it won't do the variable
interpolation I want either.

Below is the code that is causing the problem.

I think the only way to resolve this is to cut the code below and move it to
the perl code where I can use interpolation (which I would prefer not to do
because it will really clutter up the main program -- oh well).

Can anyone suggest a better approach?

Thanks,
Siegfried


if (document.images) {
one_on = new Image ();
one_on.src = "http://www.decisionchannel.com/breturn2.gif";
one_off = new Image();
one_off.src = "http://www.decisionchannel.com/return2.gif";
two_on = new Image ();
two_on.src = "http://www.decisionchannel.com/f_bwhatbut2.gif";
two_off = new Image();
two_off.src = "http://www.decisionchannel.com/f_whatbut2.gif";
.... // more of the same
}

Shaun Fryer

2004-11-22, 8:55 pm

On Mon, Nov 22, 2004 at 01:21:35PM -0700, Siegfried Heintze wrote:
> In my perl script I have put my javascript code in a separate file with the
> extension of js. I reference it with a javascript tag in the HTML. I have
> some code in there that is not in a procedure: it just executes prior to the
> body of the page loading as inline statements.
>
> Now I discover I need some of those URLs in my external javascript file to
> change depending on some perl parameters.
>
> This is not a procedure so I cannot just change some javascript procedure
> parameters. Perl does not process this file so it won't do the variable
> interpolation I want either.
>
> Below is the code that is causing the problem.
>
> I think the only way to resolve this is to cut the code below and move it to
> the perl code where I can use interpolation (which I would prefer not to do
> because it will really clutter up the main program -- oh well).
>
> Can anyone suggest a better approach?


I'd suggest loading the js file using HTML::Template. Substitute the code you
want with a <TMPL_VAR />, and write some logic so that if the query string
contains foo.js (a slight oversimplification) that it loads that template,
prints it, then exits. Does that make sense to you? Anyone else?

--
=====================
Shaun Fryer
=====================
http://sourcery.ca/
ph: 416-544-9461
=====================

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com