Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Separate Javascipt code from pure Perl code

4 views
Skip to first unread message

Nikos

unread,
May 7, 2007, 3:58:00 AM5/7/07
to
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[i].selected) {
var item = popup.options[i].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[i].selected) {
olist.options[i] = null;
}
}
olist.selectedIndex = -1;
}
function selectAll() {
var olist = document.getElementById('ordered_items');
for (var i = 0; i<olist.options.length; i++) {
olist.options[i].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-
>{date} !
Προηγούμενος αριθμών επισκέψεων => $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
#===============================================================================

Pertti Kosunen

unread,
May 7, 2007, 7:06:09 AM5/7/07
to
Nikos wrote:
> print start_html( -script => $js,
> -style=>'/data/css/style.css',

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

Nikos

unread,
May 7, 2007, 7:11:16 AM5/7/07
to

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....

Pertti Kosunen

unread,
May 7, 2007, 8:08:41 AM5/7/07
to
Nikos wrote:
> 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.

Glenn Jackman

unread,
May 7, 2007, 8:10:43 AM5/7/07
to
At 2007-05-07 07:11AM, "Nikos" wrote:
> 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....

Don't rely on Perl variables. Pass parameters to your javascript
functions.


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Nikos

unread,
May 7, 2007, 12:57:11 PM5/7/07
to
On May 7, 2:06 pm, Pertti Kosunen <pertti.kosu...@pp.nic.fi> wrote:

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?

0 new messages