CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Help with CGI.PM and javascript

 



mk3lai
Novice

Jun 18, 2010, 9:23 AM

Post #1 of 17 (3056 views)
Help with CGI.PM and javascript Can't Post

I am in the process of converting some perl webpages to the cgi.pm format. I am stomped at the javascript section so I hope someone can help me.

In one of the perl webpages, the javascript was written as:

print "<script language=\"javascript\">\n";
print "function abc {\n";
if (condition) {
print some javascript code...
} else {
print some other javascript code...
}
print the rest of javascript code

I know that in cgi.pm, javascript is written in the format of:

my $JSCRIPT=<<EOF;

function abc() {
javascript code
}

EOF
;

and $JSCRIPT is defined in start_html. However, how would I write $JSCRIPT where I want to write out different javascript codes based on different conditions in perl?

Thanks in advance.


Bianca
User

Jun 18, 2010, 9:58 AM

Post #2 of 17 (3054 views)
Re: [mk3lai] Help with CGI.PM and javascript [In reply to] Can't Post

Why do you want to "translate" the conditional section? CGI.pm only does simple standards, no conditional code. Let the conditional code like it is.


mk3lai
Novice

Jun 18, 2010, 10:03 AM

Post #3 of 17 (3052 views)
Re: [Bianca] Help with CGI.PM and javascript [In reply to] Can't Post

Well, it's not that I "want" to do it but I was told to investigate if it is possible to translate these kind of conditional javascripts to the new cgi.pm format.


Bianca
User

Jun 18, 2010, 10:05 AM

Post #4 of 17 (3050 views)
Re: [mk3lai] Help with CGI.PM and javascript [In reply to] Can't Post


In Reply To
new cgi.pm format.

What's meant by "new" cgi.pm format???
Please give an example.


mk3lai
Novice

Jun 18, 2010, 10:09 AM

Post #5 of 17 (3047 views)
Re: [Bianca] Help with CGI.PM and javascript [In reply to] Can't Post

When I said "new" cgi.pm format, I meant changing the "old" method (as in printing out each javascript and html code line by line) vs the "new" method of using cgi.pm. Sorry if the term "new" is misleading and you can ignore the term.

The example is in my first post.


Bianca
User

Jun 18, 2010, 10:24 AM

Post #6 of 17 (3042 views)
Re: [mk3lai] Help with CGI.PM and javascript [In reply to] Can't Post

OK, I understand.
The shown conditional lines I would print line by line as before. CGI.pm is not able to do those conditionals.
And it's not wrong or bad to print line by line. Don't feel bad to do it in that way.
CGI.pm is not a programming roboter, it's only a tool to print html tags with some less source code and perfect headers/meta's etc.

For example: I have a script where I need serveral <input type=hidden name=samename value=differentvalue> Tags to build a array in the form.
CGI.pm does not exactly takes the right value, it takes instead the value of the field bevor created. I don't know why it is, I fear it's a bug. And so I'm not ashamed to print the line manually.

Edit additional: It's not core competence of CGI.pm to create JS in all appreciative forms :)


(This post was edited by Bianca on Jun 18, 2010, 10:28 AM)


mk3lai
Novice

Jun 18, 2010, 10:28 AM

Post #7 of 17 (3039 views)
Re: [Bianca] Help with CGI.PM and javascript [In reply to] Can't Post

Thanks for the advice.


FishMonger
Veteran / Moderator

Jun 18, 2010, 10:49 AM

Post #8 of 17 (3035 views)
Re: [mk3lai] Help with CGI.PM and javascript [In reply to] Can't Post

When using CGI.pm, you don't have to put the javascript code in the head section, which is what is done when specifying it in start_html(). You can output it anywhere/anytime prior to the closing body tags. So, you CAN use CGI.pm to output your javascript.

If you post a short but complete script that demonstrates your problem, then we can guide you to the proper solution.


(This post was edited by FishMonger on Jun 18, 2010, 10:49 AM)


mk3lai
Novice

Jun 18, 2010, 2:18 PM

Post #9 of 17 (3026 views)
Re: [FishMonger] Help with CGI.PM and javascript [In reply to] Can't Post

Thanks for the tip, FishMonger. The actual script is really very similar to the example in the first post but here is some of the code:


Code
print "<script language=\"javascript\">\n"; 
print "function submitIT() {\n";
if ((substr($app,1,1) ne "xxx") && ($app ne "xxx")) {
print " if (formuser.uid.value != \"\") {\n";
print " $menu\n";
print " formuser.action=\"/abc.cgi\";\n";
print " formuser.submit();\n";
print " return false;\n";
print " }\n";
}
print " if (formdetail.detail.value == \"\") {\n";
print " if (formsite.site.value == \"\") {\n";
print " alert(\"You must enter a site or details\")\n";
print " formsite.site.focus();\n";
print " return false;\n";
print " }\n";
print " $menu\n";
if ($app eq "xxx") {
print "formsite.action=\"/def.cgi\";\n";
}
elsif ($app eq "xxx") {
print "formsite.action=\"/efg.cgi\";\n";
}


I was hoping to be able to rewrite this javascript with cgi.pm...if possible.


Bianca
User

Jun 18, 2010, 2:29 PM

Post #10 of 17 (3025 views)
Re: [mk3lai] Help with CGI.PM and javascript [In reply to] Can't Post

It's clear JS Code. There is no need to "rewrite"!!
What do you hope to make better if using CGI.pm?
Is that a work for us or for you?

Edit add: Don't change running system!
Means don't change more really necessary!


(This post was edited by Bianca on Jun 18, 2010, 2:32 PM)


mk3lai
Novice

Jun 18, 2010, 2:35 PM

Post #11 of 17 (3022 views)
Re: [Bianca] Help with CGI.PM and javascript [In reply to] Can't Post

With all due respect, I was responding to FishMonger's request for some codes. If you do not find any use in rewriting it then that's your opinion...others may feel it looks cleaner. If you feel it is work for you then you don't need to respond. But I am just exploring the technical possibilities regardless whether it is of "use" or not.


FishMonger
Veteran / Moderator

Jun 18, 2010, 3:25 PM

Post #12 of 17 (3018 views)
Re: [mk3lai] Help with CGI.PM and javascript [In reply to] Can't Post

If you want to clean it up, then use a here doc instead of those messy print statements.

e.g.,

Code
my $javascript = <<"JS"; 
if (formdetail.detail.value == "") {
if (formsite.site.value == "") {
alert("You must enter a site or details")
formsite.site.focus();
return false;
}
$menu
JS

print $javascript;

# or if you feel that you want to use the cgi module
print $cgi->pre($javascript);


If you don't want to use a here doc, then do this.

Code
print qq(  if (formdetail.detail.value == "") {\n), 
qq( if (formsite.site.value == "") {\n),
qq( alert("You must enter a site or details")\n),
qq( formsite.site.focus();\n),
qq( return false;\n),
qq( }\n),
qq( $menu\n);



(This post was edited by FishMonger on Jun 18, 2010, 3:27 PM)


mk3lai
Novice

Jun 18, 2010, 3:31 PM

Post #13 of 17 (3015 views)
Re: [FishMonger] Help with CGI.PM and javascript [In reply to] Can't Post

Thanks FishMonger. I know of this format. But I wonder if it is possible to incorporate Perl code inside


Code
my $javascript = <<"JS";  
if (formdetail.detail.value == "") {
if (formsite.site.value == "") {
alert("You must enter a site or details")
formsite.site.focus();
return false;
}
$menu

if (x) { <== perl code
some javascript code
} else {
other javascript code
}
JS



FishMonger
Veteran / Moderator

Jun 18, 2010, 4:01 PM

Post #14 of 17 (3013 views)
Re: [mk3lai] Help with CGI.PM and javascript [In reply to] Can't Post

Yes, you can incorporate code inside a here doc, but the required syntax gets very messy and no one would recommend doing it.


mk3lai
Novice

Jun 18, 2010, 4:03 PM

Post #15 of 17 (3011 views)
Re: [FishMonger] Help with CGI.PM and javascript [In reply to] Can't Post

Is it possible to give a small example...just for my curiosity? Thanks.


FishMonger
Veteran / Moderator

Jun 18, 2010, 4:16 PM

Post #16 of 17 (3010 views)
Re: [mk3lai] Help with CGI.PM and javascript [In reply to] Can't Post

I don't recall the details of how to code it, but if you search the archives at perl.beginners news group, you'll find a good discussion on its syntax and why you shouldn't do it.

Here's the google portal.
http://groups.google.com/group/perl.beginners/topics?hl=en

A large portion of the people providing help on that NG are well known Perl module and Perl book authors.


mk3lai
Novice

Jun 18, 2010, 4:17 PM

Post #17 of 17 (3008 views)
Re: [FishMonger] Help with CGI.PM and javascript [In reply to] Can't Post

Alright, thanks.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives