473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Generating javascript through perl

79
Hello,

I have a perl file that generates an html file to use on a webpage. I have to add some javascript to the html part of the perl file.

I already have some code to do that, but here is the issue: the javascript needs to grab a parameter from the perl information.

eg., lets say I need to run javascript that uses a directory name for example, and that directory can change. So I cannot hardcode it...instead, I have a variable called $dir to store that information. How can I generate the javascript part and pass it the contents of $dir, so that the javascript points to the correct directory when executing?

Thanks
Oct 11 '07 #1
11 1543
KevinADC
4,059 Expert 2GB
if $dir is defined before the javascript is printed:

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2.   function foo() {
  3.      var dir = $dir
  4.      ......
  5.   }
  6. </script>
  7.  

if $dir is not defined before the javascrpt gets printed I don't know how you can do it.
Oct 11 '07 #2
ahammad
79
This is what I have.

So would your method work, ie adding var name = $someName in the execCmd function?

Expand|Select|Wrap|Line Numbers
  1. {print "Content-type:text/html\n\n";
  2.  
  3. $someName = "name";
  4. $someUser = "user";
  5.  
  6. {
  7.     print <<EOS;
  8.  
  9.             <head>
  10.                 <title> TEST </title>
  11.                 <style type="text/css">
  12.                         <!--
  13.                         .style1 {
  14.                             color: #0000FF;
  15.                             font-weight: bold;
  16.                             font-size: 30px;
  17.                         -->
  18.                         </style>
  19.  
  20.                 <script type="text/javascript">
  21.                     function execCmd() 
  22.                     { 
  23.                         var shell = new ActiveXObject("WScript.shell"); 
  24.                         shell.run(''clt kpp Build \\"<someUser>\\" name- <someName>.ext'); 
  25.                     }
  26.                 </script>
  27.             </head>
  28.  
  29.             <body onLoad = "execCmd()" alink = blue vlink = blue background = "../image/bkgr.jpg">
  30.  
  31.             </body>
  32. EOS
  33. }
<someName> and <someUser> are the variables that need to change. Usually when I print stuff, I do print($someName . "random text" . $someUser . "\n");

Essentially, I need to do the samething in the execCmd. I need to replace <someName> and <someUser> with the perl equivalent ($someName...). So how would I do that?
Oct 11 '07 #3
KevinADC
4,059 Expert 2GB
Sorry mate, I'm not interested in helping people that cross-post their questions. Go read the replies on the other forum.
Oct 11 '07 #4
Kelicula
176 Expert 100+
Hello,

I have a perl file that generates an html file to use on a webpage. I have to add some javascript to the html part of the perl file.

I already have some code to do that, but here is the issue: the javascript needs to grab a parameter from the perl information.

eg., lets say I need to run javascript that uses a directory name for example, and that directory can change. So I cannot hardcode it...instead, I have a variable called $dir to store that information. How can I generate the javascript part and pass it the contents of $dir, so that the javascript points to the correct directory when executing?

Thanks
Maybe you could use the CGI module.
Take a look at this example:

I define some variables in perl first.
Then print the header.
Then create a "here document" containing all the javascript that will occur up until the point that I want to include a perl variable.
When you want to include a perl variable terminate the here doc, and add the variable to the script scalar, in this case "$JSCRIPT".
Notice the ".=" (add to the existing scalar $JSCRIPT)
The add another here doc to $JSCRIPT until you want to add another perl variable.

Also notice that when I declared the perl variable I used "'...'" what I am doing is storing a set of single quotes within the perl variable, so that the javascript will not think that hey is an object. This may not be needed depending on what the context of the javascript is. If I where inserting something into javascript that didn't need to be a string, I wouldn't need the single quotes.

If you run this code you will find that it works.
The resulting page will create an alert that says "hey".


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use CGI qw(:standard);
  6.  
  7. my $it = "'hey'";
  8. print header();
  9.  
  10. my $JSCRIPT =<<SRC;
  11.  
  12. function doIt(){
  13.  
  14. alert(
  15. SRC
  16. $JSCRIPT .= $it;
  17.  
  18. $JSCRIPT .=<<SRC;
  19. );
  20.  
  21. // some more code here
  22. }
  23. SRC
  24.  
  25. print start_html(-script=>$JSCRIPT,onload=>'javascript:doIt();');
  26.  
  27. print end_html();
  28.  
The real power here comes from the CGI module.
It allows me to include the $JSCRIPT var into the head of the resulting page, also I am able to add an onload event to the body tag.

Read more about the CGI module HERE
Oct 12 '07 #5
ahammad
79
Sorry mate, I'm not interested in helping people that cross-post their questions. Go read the replies on the other forum.
I wasn't aware that "cross-posting" questions is enough to annoy people. As far as I'm concerned, the more help/options that I can get the better. There is nothing wrong with this method of learning, which incidentally worked fantastically for me.

You're an expert at this, I'm not. The project that I'm working on was shoved into my pile before I knew what perl was, and this is really the only way I can pick up on the more advanced stuff. I was simply looking for different answers so that I'm able to pick the best method for the purposes of my project

I, just like you, surf more than one developer forum. I'm pretty sure if you had questions that you really need answers for, you'd do the same thing. I hope you understand where I'm coming from.

In any case, thanks for your initial reply.

Kelicula:
As for the CGI module, I've been reading up on it and it seems like it's a perfect solution. I will need to use it for other cases eventually, so I might as well start learning it now. I haven't started the coding part, but your example is very well written and easy to understand. Thanks for all the help.
Oct 12 '07 #6
numberwhun
3,509 Expert Mod 2GB
I wasn't aware that "cross-posting" questions is enough to annoy people. As far as I'm concerned, the more help/options that I can get the better. There is nothing wrong with this method of learning, which incidentally worked fantastically for me.

You're an expert at this, I'm not. The project that I'm working on was shoved into my pile before I knew what perl was, and this is really the only way I can pick up on the more advanced stuff. I was simply looking for different answers so that I'm able to pick the best method for the purposes of my project

I, just like you, surf more than one developer forum. I'm pretty sure if you had questions that you really need answers for, you'd do the same thing. I hope you understand where I'm coming from.

In any case, thanks for your initial reply.

Kelicula:
As for the CGI module, I've been reading up on it and it seems like it's a perfect solution. I will need to use it for other cases eventually, so I might as well start learning it now. I haven't started the coding part, but your example is very well written and easy to understand. Thanks for all the help.
I think that the point KevinADC was trying to make was that you already had an answer to your question at the other forum where you posted the same question and was just letting you know that he wasn't going to provide the same answer here as it was already given.

Cross posting is mostly annoying to others when it happens and the person trying to provide the help sees that is has happened, yet the poster in question does not post to the other forums ( as a courtesy) that they have found an answer on another forum.

You have to look at things from an angle other than that of the person in need.

Regards,

Jeff
Oct 12 '07 #7
ahammad
79
I think that the point KevinADC was trying to make was that you already had an answer to your question at the other forum where you posted the same question and was just letting you know that he wasn't going to provide the same answer here as it was already given.

Cross posting is mostly annoying to others when it happens and the person trying to provide the help sees that is has happened, yet the poster in question does not post to the other forums ( as a courtesy) that they have found an answer on another forum.

You have to look at things from an angle other than that of the person in need.

Regards,

Jeff
I understand that, but the questions were posted at the same time. The other forum simply got to the answer quicker.

In any case, I apologize for any inconvenience. Like I said, I take all suggestions, regardless of whether or not I solved the problems. I keep all the replies for future reference anyway. If a better solution comes along after a problem has been solved, I usually do change what I have to make it better.
Oct 12 '07 #8
numberwhun
3,509 Expert Mod 2GB
I understand that, but the questions were posted at the same time. The other forum simply got to the answer quicker.

In any case, I apologize for any inconvenience. Like I said, I take all suggestions, regardless of whether or not I solved the problems. I keep all the replies for future reference anyway. If a better solution comes along after a problem has been solved, I usually do change what I have to make it better.
Think of it like this. You are in a room full of people all talking about the same thing. Someone walks in and asks a question of everyone there. All of the people in the room have the answer at the same time. Would you speak up and give the same answer as the rest of just go back to your discussion, knowing that the others have answered it the same as you would have.

That is how KevinADC feels and I back him up on that. Personally, I stick to the one forum at a time rule. If you know of a forum that will consistently answer your questions quicker (maybe due to a larger user base), then ask there first. Then, if no joy is received there, move on to another forum.

You will be suprised how many people take Kevins stance. I do!

Regards,

Jeff
Oct 12 '07 #9
Kelicula
176 Expert 100+
If a better solution comes along after a problem has been solved, I usually do change what I have to make it better.

I think I have a better solution.

If you include the perl variables within the javascript in double quotes I think it will interpolate them for you.

ie:
Expand|Select|Wrap|Line Numbers
  1. my $user = param('user');
  2. my $time = localtime;
  3.  
  4. my $JSCRIPT = <<SRC;
  5.  
  6.  
  7. var user ='"$user"';
  8.  
  9. document.theDiv.innerHTML = 'hello '+user+' I see it is '+"$time"+' oclock';
  10.  
  11.  
  12. SRC
  13.  
  14.  
Once again I have added single quotes to tell jscript that it's a string, but this I put them on the outside, hoping that perl will interpolate the $user var, then encapsulate it in the singles.
(not always necessary)

Notice the double quoted $time var.

This code is untested though. I'm at work and can't try it out till I get home...

Hope it helps..
Oct 12 '07 #10
KevinADC
4,059 Expert 2GB
I wasn't aware that "cross-posting" questions is enough to annoy people. As far as I'm concerned, the more help/options that I can get the better. There is nothing wrong with this method of learning, which incidentally worked fantastically for me.

You're an expert at this, I'm not. The project that I'm working on was shoved into my pile before I knew what perl was, and this is really the only way I can pick up on the more advanced stuff. I was simply looking for different answers so that I'm able to pick the best method for the purposes of my project

I, just like you, surf more than one developer forum. I'm pretty sure if you had questions that you really need answers for, you'd do the same thing. I hope you understand where I'm coming from.

In any case, thanks for your initial reply.
Don't take it personally. My opinion about cross-posting a question on multiple forums is just that, it's mine. I see no reason to defend or explain my reasons to you or anyone else.

However, I do apologize for my other comment. I should not have told you to "go read the replies on the other forum". That was inappropriate of me and I am sure sounded very rude. I did not mean it to be rude, but I can see now that my words were poorly choosen and it was very inappropriate of me to tell you what to do.

You are free to post a question on as many forums as you like, and I am free to decide not to answer them.

My apology for being rude to you.
Kevin
Oct 13 '07 #11
ahammad
79
No worries.

Kelicula: I'm trying that right now, I hope it works. The other things I tried did not end up working properly. The code is read, but the command isn't executed for some reason.
Oct 15 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
3
by: Tommo | last post by:
Hello All, I am a still learning so be easy on me. I am trying to get some code to work that is using JS and Perl/CGI, I am using AS Perl and an Apache Server on XP as the webserver. Can anyone...
6
by: Richard Trahan | last post by:
I want a js function to call a Perl script residing on a server. The Perl script will return a string, to be used by the js. Pseudo code: <script> stringvar = perlfunc_on_server(stringarg)...
3
by: phal | last post by:
Hi all; I code Perl for CGI, I using regular expression to check the validation of user input, because the form is small and it run only from my own computer, anyways if many people using my...
10
by: Alex | last post by:
I'm not exactly sure how to use these two languages together. It seems to me that they both totally different things. PERL = databases, heavy processing, server functions. But I can also write...
4
by: shaun roe | last post by:
I have a directory which contains many xml files as a result of some analysis. I would like to have an XSL which loops over all these files and generates a web page indicating some summary...
3
by: Nathan Gilbert | last post by:
I am wanting to use javascript to select between different *.css files dependent on the user's browser. I am also wanting to generate the html document containing this javascript dynamically using...
10
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem...
3
by: happyse27 | last post by:
Hi All, I am creating the perl script using html form(with embedded javascript inside). When using this html form with javascript alone, it works where the form validation will pop up...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.