user1445267
user1445267

Reputation: 97

javascript inside perl script

I have created a perl script which is getting run time data from server and displaying on a html page. I am not using any databse there.

I have to link another perl script with 1 javascript variable. I want to make that value global so that I can access from anywhere.

Here is the problem

For testing when I include that javascript function inside perl script it alerts me passed value but if I put that function in a js file and call it, it doesn't alert anything. I tried calling js file from multiple locations but no luck :(

Can anybody help me on this asap?

this is code from js file

`$(document).ready(function(){
function aabcd(){
var y=document.getElementById("mnth").value;
alert(y);
}
});
`

Here is the full code.

I am facing problem here I am unable to call this test.js file. If I include this file's contents in this perl script then it works fine but I want to use via file so that I can use those variable as globally. `

#!/usr/bin/perl
#!/bin/ksh


use CGI qw(:standard escapeHTML);
print <<EOH;
Content-Type: text/html; charset=ISO-8859-1

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en" />
<title> ITSS UNIX report</title>

<style type="text/css">
h1 { font-size: 24px; }
body {
     font: 13px tahoma;
     background: #ffffff;
      margin: 1em 2em;
        padding: 0em;
     }
</style>
<link rel="stylesheet" type="text/css" href="/example1.css" />
<script type="text/javascript" src="/DropMenu1.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" src="/test.js"></script>
</head>
<body>


<TABLE WIDTH="100%" cellSpacing=0 cellPadding=1 align=center border=0 style='border: 1px solid; border-width: 0 0 0 0; border-style:solid; margin: 0; padding: 0;' bordercolor=#B
DBDBD>
<TR>
<TD width=100% BGCOLOR=#000066 ALIGN=center><FONT style='font-size: 28.0pt;mso-bidi-font-size: 11.0pt;font-family: tahoma;color: #FFFFFF;background-color=#000066'>User Audit Sec
urity Report</TD>
</TR>
</TABLE>
<form name="myform">
<select name="year" id="year">
<option value="2011">2011</option><option value="2012">2012</option></select>
<select name="mnth" id="mnth" onchange="return aabcd();">
<option value="-1" class="item1" >Click for Month</option>
<option value="Jan" class="item2" >January </option>
<option value="Feb" class="item2" >February</option>
<option value="Mar" class="item2" >March </option>
<option value="Apr" class="item2">April</option>
<option value="May" class="item2" >May </option>
<option value="Jun" class="item2" >June </option>
<option value="Jul" class="item2" >July</option>
<option value="Aug" class="item2" >August </option>
<option value="Sep" class="item2" >September</option>
<option value="Oct" class="item2" >October</option>
<option value="Nov" class="item2" >November</option>
<option class="item2" value="Dec">December</option></select></td></tr>
<input id="sub" value="Submit" type="button">
</form>

Upvotes: 3

Views: 2641

Answers (1)

sockmonk
sockmonk

Reputation: 4255

If you look at the perldoc for the CGI module, this is the recommended way to include javascript files:

print $q->start_html(-title=>'The Riddle of the Sphinx',
                 -script=>[
                           { -type => 'text/javascript',
                             -src      => '/javascript/utilities10.js'
                           },
                           { -type => 'text/javascript',
                             -src      => '/javascript/utilities11.js'
                           },
                           { -type => 'text/jscript',
                             -src      => '/javascript/utilities12.js'
                           },
                           { -type => 'text/ecmascript',
                             -src      => '/javascript/utilities219.js'
                           }
                        ]
                    );

$q would hold a CGI object, and should also be used to construct the rest of the HTML. That should greatly improve its chances of being valid. You'll also want to verify that '/test.js' is the correct URL to fetch the javascript file, and confirm by checking your server logs that it's getting served back to the browser. You can also check in Firebug that your browser is requesting and receiving it.

Finally, while CGI was very popular 10 or 12 years ago, there are many more modern ways to develop a web application in perl today. Check on cpan for CGI::Application and Dancer, for instance. Using something like Template::Toolkit may make your life easier as well.

Upvotes: 1

Related Questions