Nik,
Various monks, including myself, spent over an hour in the CB discussing this with you a couple of days ago. At the time you said you were going to take our advice about using HTML::Template to separate the html/JavaScript code from your Perl code, since we explained at great length what would be involved in this, how to achieve what you are trying to do easily and what other tools you could use to do the job. Has this been (once again) a waste of our time? It would certainly seem so. Also, you have been posting on this forum for years, please link to things properly, you have put a link within code tags for some reason.
Martin | [reply] |
Separate Javascript code from pure Perl code
Just save your JS code in separate .js files and load them as needed. Unless you specifically have to generate that code dynamically. I can hardly imagine such a situation.
In the past I've been the one "bashing" you for double posting here and in clpmisc with no mutual acknowledgement. (BTW: I won't have access to a proper news server and I find GG's UI to be too clunky for me.) Now I see you're including references when doing so: again, well done! Just bring the thingie one step further: when inserting a url like the above, it would be best not to put it in <code> tags, but to make it a proper link. There are many smart ways to do so in the Monastery. In this particular case I would have written something along the lines of
Please see this [http://groups.google.com/group/comp.lang.perl.misc/br
+owse_thread/thread/4d2219139165592c/#|post] on clpmisc and suggest so
+mething.
That renders as "Please see this post on clpmisc and suggest something."
I would also repeat the actual content of the linked to post, removing any reference to the original destination and adding PM-specific formatting to increase readability. | [reply] [d/l] [select] |
Please take the advice of all the other posters to this thread. They gave good advice.
It seems that you want to set a variable for your char_by_chars.js script file.
You can't shoehorn a variable's content into a file that is referenced by the browser via a src
attribute, other that opening this file, writing the variable's content to it, and close the file.
Which surely is not what you want.
But you can include verbatim Javascript with the content of a variable in your page, before the
directives containing src tags:
my $data = 'Some text in a variable defined before loading script file
+s';
print start_html(
-script => [
"var textToShow = '$data';\n",
{
-language => 'JAVASCRIPT',
-text => "var textToShow = \"$data\";",
},
{
-language => 'JAVASCRIPT',
-src => '/data/scripts/char_by_char.js'
},
{
-language => 'JAVASCRIPT',
-src => '/data/scripts/orderlist.js'
}
],
-style => '/data/scripts/style.css',
-title => 'Order Project!'
);
__END__
<!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-U
+S">
<head>
<title>Order Project!</title>
<link rel="stylesheet" type="text/css" href="/data/scripts/style.css"
+/>
<script type="text/javascript">//<![CDATA[
var textToShow = 'Some text in a variable defined before loading scrip
+t files';
//]]></script>
<script type="text/javascript"></script>
<script src="/data/scripts/char_by_char.js" type="text/javascript"></s
+cript>
<script src="/data/scripts/orderlist.js" type="text/javascript"></scri
+pt>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1
+" />
</head>
<body>
Since your variable textToShow thusly is defined before loading any JavaScript file,
It will be available to your functions inside those files - if you don't re-define that
variable in your JavaScript file, that is.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [d/l] [select] |
Firsly a HUGE thanks to you shmem for being able to understand EXACTLY my needs and also willing to help me implement it! I want you to knwo that i really apreciate your help given a great deal!
I used the above snippet inside my index.pl but unfortunately non of the 2 javascripts seems to appear(called), down kno why, youc an check them though by going to http://nikos.no-ip.org
It seems that you want to set a variable for your char_by_chars.js script file.
Yes, exactly! This is a cool javascript displaying the contents of variable $data character_by_character. So you can see why it is essential for me to pass this variable from index.pl => char_by_char.js
Also variables must be passed to the othe script you have made yourself for helping me(you know the i liek it to call it orderlist js) so to be functional. So we also have to pass values there....
ps. Do the javascript files are the first thing that gets executed when index.pl loads? if yes, by what order? And also if they are then how do they contain the correct value of $data showing in the following code, which is presented later on index.pl?!?
print a( {href=>'/cgi-bin/register.pl'}, img{src=>'/data/images/reg.j
+pg'} );
my @userlist = @{ $db->selectcol_arrayref("SELECT username FROM users"
+) };
print start_form( action=>'/cgi-bin/index.pl' );
print h1( {class=>'lime'}, "Κάνε LOGIN γ
+ια να δεις τις
+ παραγγελίες &
+#963;ου μέχρι στι&#
+947;μής => ",
popup_menu( -name => 'userlogin', -val
+ues => \@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'}, "Δεν υ
+πάρχει χρήστ&#
+951;ς με όνομα: $userlogin" )
+;
exit;
}
$select = $db->prepare( "SELECT counter, host, DATE_FORMAT(date, '
+%a %d %b, %h:%i') AS date FROM guestlog WHERE username=?" );
$select->execute( $userlogin );
$row = $select->fetchrow_hashref;
if( $select->rows )
{
$data = "Καλώς ήλθ^
+9;ς $userlogin! Χαίρομαι
+ που βρίσκεις
+την σελίδα εν&
+#948;ιαφέρουσα.
Τελευταία
+; φορά ήρθες εδ
+;ώ ως $row->{host} στις $row->{dat
+e} !
Προηγούμε
+;νος αριθμών ε
+πισκέψεων => $row->{coun
+ter}
Τί θα πάρε	
+53;ς σήμερα !?";
$select = $db->prepare( "UPDATE guestlog SET username=?, count
+er=counter+1, date=? WHERE host=?" );
$select->execute( $userlogin, $date, $host );
}
}
else
{
if( $host eq "Νίκος" ) {
$data = "Γειά σου Ν	
+53;κόλα! Πώς πάν	
+49; τα κέφια? ;-)";
}
else {
$data = "Γειά σου $host!
Έρχεσαι γ
+;ια 1η φορά εδώ !!
Ελπίζω να β
+;ρείς όλο το λο
+;γισμικό που ε
+πιθυμείς :-)";
}
unless( $host eq "Νίκος" ) {
$select = $db->prepare( "INSERT INTO guestlog (username, count
+er, host, date) VALUES (?, ?, ?, ?)" );
$select->execute( 'Guest', 1, $host, $date);
}
}
| [reply] [d/l] |
ps. Do the javascript files are the first thing that gets executed when index.pl loads? if yes, by what order?
Yes, JavaScript is parsed and executed at load time. And it is executed in the order it appears.
And also if they are then how do they contain the correct value of $data showing in the following code, which is presented later on index.pl?!?
Simple - they don't. If you want to assign data to a JavaScript variable with the technique I showed you,
the data must be available beforehand. Once you have output your start_html, the data you aquire later
in your script doesn't travel after the output already written, overtaking it and magically inserting
itself into the place you want it to be. You will have to re-organize your index.pl.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [d/l] [select] |