Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating javascript through perl

Status
Not open for further replies.

ahammad

Programmer
Sep 24, 2007
10
FR
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
 
If you are doing this out of perl then you can just print the variable as part of the javascript.

i.e.
$var = "/path/to/somethin";

print "<javascript>do some javascripty on $var</jvascript>";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
print "<javascript>do some javascripty on $var</jvascript>";

So is <javascript> defined in perl or do I have to do something else?

Code:
<head>
	<script type="text/javascript">
	
	function execCmd() 
	{ 
		var shell = new ActiveXObject("WScript.shell"); 
		shell.run('clt kpp Build \\"<someUser>\\" name- <someName>.ext'); 
	}
		
	</script>

</head>

<body>
	<INPUT TYPE="button" WIDTH=105 ALT="Starflower" ALIGN="ABSMIDDLE" onclick="execCmd()" value="Click me!">
</body>

This is what I had originally, which doesn't work. This is pure html that is within a perl file, so I'm thinking that that's why it isn't working. <someUser> and <someName> are the variables to be changed. So how would I change it so it would function?

Thanks a lot.
 
are you doing like print <<EOF ? If so you will need to change that to a regular print statement to have the variables show up.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Yes, I am using EOF.

So how would you convert var shell = new ActiveXObject("WScript.shell");
shell.run('clt kpp Build \\"<someUser>\\" name- <someName>.ext'); into a regular print statement? Where would the <javascript> tags go?

Like I said, I have no idea where to begin. I don't know anything about javascript, I just need to integrate it within perl code.
 
BTW, this is what I had:

print "Content-type:text/html\n\n";

$someName = "name";
$someUser = "user";

[/code]{
print <<EOS;

<head>
<title> TEST </title>
<style type="text/css">
<!--
.style1 {
color: #0000FF;
font-weight: bold;
font-size: 30px;
-->
</style>

<script type="text/javascript">
function execCmd()
{
var shell = new ActiveXObject("WScript.shell");
shell.run(''clt kpp Build \\"<someUser>\\" name- <someName>.ext');
}
</script>
</head>

<body onLoad = "execCmd()" alink = blue vlink = blue background = "../image/bkgr.jpg">

</body>
EOS
}
Code:
<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 same thing in the execCmd. I need to replace <someName> and <someUser> with the perl equivalent ($someName...). So how would I do that?
 
print '

<head>
<title> TEST </title>
<style type="text/css">
<!--
.style1 {
color: #0000FF;
font-weight: bold;
font-size: 30px;
-->
</style>

<script type="text/javascript">
function execCmd()
{
var shell = new ActiveXObject("WScript.shell");
shell.run(\'clt kpp Build \\"<'
,$someUser,'>\\" name- <',$someName,'>.ext\');
}
</script>
</head>

<body onLoad = "execCmd()" alink = blue vlink = blue background = "../image/bkgr.jpg">

</body>
';


You might have to double check.. but anything inside of single ticks doesn't interpret variables.. so you stop then do a comma then print your var then do another comma a single ticket and continue. You might want to look at your shell line also because you had 2 single ticks at the beginning of it. I removed one and escaped the other 2.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
If you are doing a lot of this kind of thing, then I would suggest you look at Template::Toolkit or one of the other templating modules on CPAN. This allows you to separate your templates from your code (always a good thing).

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Yeah I have made alot of use out of HTML::Template, however there are better modules available such as Mason and Template::Toolkit
 
I use qq~~ to print out large blocks of HTML code.

Code:
print qq~
<html>
            <head>
                <title> TEST </title>
                <style type="text/css">
                        <!--
                        .style1 {
                            color: #0000FF;
                            font-weight: bold;
                            font-size: 30px;
                        -->
                        </style>
                        
                <script type="text/javascript">
                    function execCmd()
                    {
                        var shell = new ActiveXObject("WScript.shell");
                        shell.run(\'clt kpp Build \\"<'
,$someUser,'>\\" name- <',$someName,'>.ext\');
                    }
                </script>
            </head>
            
            <body onLoad = "execCmd()" alink = blue vlink = blue background = "../image/bkgr.jpg">

            </body>
~;

That way, variables get interpolated without having to add more print statements or concatenate them (which looks messy when reading over your code), and you don't run into any weird syntax errors. Just, if you use the tilde ~ like I do, everytime you want a ~ to appear in your page, just escape it with \~. Other than that its pretty simple to manage your page without worrying too much about escaping things. :)

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top