Separate Javascipt code from pure Perl code

N

Nikos

Is there a way to put those 2 javascript snippets that are currently
embedded inside index.pl into 1 or 2 separate files because index.pl
now has gone very huge, but also index.pl to remain functional?

#===============================================================================
my $js = <<EOH;
function addFromList() {
var popup = document.getElementById('title');
var olist = document.getElementById('ordered_items');
for (var i = 0; i<popup.options.length;i++) {
if(popup.options.selected) {
var item = popup.options.value;
// if you want to allow multiple instances
// of the same item in your list, remove the
// next three lines.
for(var j = 0; j<olist.options.length; j++) {
if (olist.options[j].value == item) return;
}
var opt = new Option(item,item,false,false);
olist.options[olist.options.length] = opt;
}
}
}
function addFromEntry () {
var olist = document.getElementById('ordered_items');
var entry = document.getElementById('requesttext');
if(entry.value != '') {
var opt = new Option(entry.value,entry.value, false,false);
olist.options[olist.options.length] = opt;
}
}
function removeFromList () {
var olist = document.getElementById('ordered_items');
for (var i = olist.options.length - 1; i>=0; i--) {
if (olist.options.selected) {
olist.options = null;
}
}
olist.selectedIndex = -1;
}
function selectAll() {
var olist = document.getElementById('ordered_items');
for (var i = 0; i<olist.options.length; i++) {
olist.options.selected = true;
}
}
EOH
#===============================================================================

print header( -charset=>'utf-8' );
print start_html( -script => $js,
-style=>'/data/css/style.css',
-title=>'Order Project!' );

print a( {href=>'/cgi-bin/register.pl'}, img{src=>'/data/images/
reg.jpg'} );

my @userlist = @{ $db->selectcol_arrayref("SELECT username FROM
users") };

print start_form( action=>'/cgi-bin/index.pl' );
print h1( {class=>'lime'}, "ÊÜíå LOGIN ãéá íá äåéò ôéò
ðáñáããåëßåò óïõ ìÝ÷ñé óôéãìÞò => ",
popup_menu( -name => 'userlogin', -
values => \@userlist ),
submit('Óýíäåóç!'));
print end_form;


my $userlogin = param('userlogin');

if ( param('Óýíäåóç!') )
{
unless( grep { $_ eq $userlogin }
@userlist ) #Check if userlogin name exists in
the drop down menu
{
print br() x 2, h1( {class=>'big'}, "Äåí õðÜñ÷åé ÷ñÞóôçò ìå
üíïìá: $userlogin" );
exit;
}

$select = $db->prepare( "SELECT host FROM guestlog WHERE
host=?" );
$select->execute( $host );

if ($select->rows)
{
$select = $db->prepare( "SELECT host, DATE_FORMAT(date, '%a %d
%b, %h:%i') AS date, counter FROM guestlog WHERE host=?" );
$select->execute( $host );
$row = $select->fetchrow_hashref;

$data = "Êáëþò Þëèåò $userlogin! ×áßñïìáé ðïõ âñßóêåéò ôçí
óåëßäá åíäéáöÝñïõóá.
Ôåëåõôáßá öïñÜ Þñèåò åäþ ùò $row->{host} óôéò $row-
Ðñïçãïýìåíïò áñéèìþíåðéóêÝøåùí => $row->{counter}
Ôß èá ðÜñåéò óÞìåñá !?";

$select = $db->prepare( "UPDATE guestlog SET date=?,
counter=counter+1 WHERE host=?" );
$select->execute( $date, $host );
}
}
else
{
if ($host eq "Íßêïò") {
$data = "ÃåéÜ óïõ Íéêüëá! Ðþò ðÜíå ôá êÝöéá? ;-)";
}
else {
$data = "ÃåéÜ óïõ $host!
¸ñ÷åóáé ãéá 1ç öïñÜ åäþ!!
Åëðßæù íá âñåßò üëï ôï ëïãéóìéêü ðïõ åðéèõìåßò :)";
}

unless ($host eq "Íßêïò") {
$select = $db->prepare( "INSERT INTO guestlog (host, date,
counter) VALUES (?, ?, ?)" );
$select->execute( $host, $date, 1 );
}
}

for ($data) {
s/\n/\\n/g;
s/"/\\"/g;
tr/\cM//d;
}

#===============================================================================
print <<ENDOFHTML;
<script type='text/javascript'>

var textToShow = "$data";
var tm;
var pos = 0;
var counter = 0;

function init()
{ tm = setInterval("type()", 50) }

function type()
{
if (textToShow.length != pos)
{
d = document.getElementById("DivText");
c = textToShow.charAt(pos++);

if (c.charCodeAt(0) != 10)
d.appendChild(document.createTextNode(c));
else
d.appendChild(document.createElement("br"));

counter++;

if (counter >= 3000 && (c.charCodeAt(0) == 10 || c == "."))
{
d.appendChild(document.createElement("br"));
d.appendChild(document.createTextNode("Press a key to
continue..."));
counter = 0;
clearInterval(tm);
document.body.onkeypress = function () {
document.getElementById("DivText").innerHTML = '';

tm = setInterval("type()", 50);
document.body.onkeypress = null; };
}
}
else
clearInterval(tm);
}
</script>

<body onload=init()>
<center>
<div id="DivText" align="Left" style="
overflow: auto;
background-image: url(/data/images/vortex.jpg);
border: Ridge Magenta 5px;
width: 1100px;
height: 100px;
color: Cyan;
font-family: Bookman;
font-size: 16px;">
</div>
ENDOFHTML
#===============================================================================
 
P

Pertti Kosunen

Nikos said:
print start_html( -script => $js,
-style=>'/data/css/style.css',

-script =>
[
{
-language=>'JAVASCRIPT',
-src=>'/scripts/script1.js'
},
{
-language=>'JAVASCRIPT',
-src=>'/scripts/script2.js'
}
],
-style...
 
N

Nikos

Nikos said:
print start_html( -script => $js,
-style=>'/data/css/style.css',

-script =>
[
{
-language=>'JAVASCRIPT',
-src=>'/scripts/script1.js'
},
{
-language=>'JAVASCRIPT',
-src=>'/scripts/script2.js'
}
],
-style...

Will the variables for example $data will be able to pass from
index.pl => script1.js => index.pl ?! because both javascript snippets
use variabels of index.pl and have action with them....
 
P

Pertti Kosunen

Nikos said:
Will the variables for example $data will be able to pass from
index.pl => script1.js => index.pl ?! because both javascript snippets
use variabels of index.pl and have action with them....

Oh yes, then 'do', 'use' etc. could do it.
 
N

Nikos

Nikos said:
print start_html( -script => $js,
-style=>'/data/css/style.css',

-script =>
[
{
-language=>'JAVASCRIPT',
-src=>'/scripts/script1.js'
},
{
-language=>'JAVASCRIPT',
-src=>'/scripts/script2.js'
}
],
-style...

Thank you Pertti!

<code>
print header( -charset=>'utf-8' );
print start_html( -script =>
[
{
-language => 'JAVASCRIPT',
-src => '/data/scripts/char_by_char.js'
},
{
-language => 'JAVASCRIPT',
-src => '/data/scripts/orderlist.js'
}
],
-style => '/data/scripts/style.css',
-title => 'Order Project!' );
</code>
This is how to call he scripts, if only i had a way to pass the $data
varibale as well including call, can you help with that too perhaps?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top