Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Separate Javascipt code from pure Perl code

Reply
Thread Tools

Separate Javascipt code from pure Perl code

 
 
Nikos
Guest
Posts: n/a
 
      05-07-2007
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
#================================================= ==============================

 
Reply With Quote
 
 
 
 
Pertti Kosunen
Guest
Posts: n/a
 
      05-07-2007
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...
 
Reply With Quote
 
 
 
 
Nikos
Guest
Posts: n/a
 
      05-07-2007
On May 7, 2:06 pm, Pertti Kosunen <(E-Mail Removed)> wrote:
> 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...


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

 
Reply With Quote
 
Pertti Kosunen
Guest
Posts: n/a
 
      05-07-2007
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.
 
Reply With Quote
 
Nikos
Guest
Posts: n/a
 
      05-07-2007
On May 7, 2:06 pm, Pertti Kosunen <(E-Mail Removed)> wrote:
> 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...


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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pure space directly inside div ignored, but pure space directlyinside span honored liketofindoutwhy@gmail.com HTML 4 03-29-2008 06:06 PM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:26 AM
Using separate classpaths for separate classes? Frank Fredstone Java 1 06-27-2006 06:46 AM
Pure functions still pure after definition Todd Aspeotis C++ 3 05-30-2005 03:53 AM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 AM



Advertisments