in reply to Separate Javascipt code from pure Perl code

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}

Replies are listed 'Best First'.
Re^2: Separate Javascipt code from pure Perl code
by Nik (Initiate) on May 07, 2007 at 18:48 UTC
    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'}, "&#922;&#940;&#957;&#949; LOGIN &#947; +&#953;&#945; &#957;&#945; &#948;&#949;&#953;&#962; &#964;&#953;&#962; + &#960;&#945;&#961;&#945;&#947;&#947;&#949;&#955;&#943;&#949;&#962; & +#963;&#959;&#965; &#956;&#941;&#967;&#961;&#953; &#963;&#964;&#953;&# +947;&#956;&#942;&#962; => ", popup_menu( -name => 'userlogin', -val +ues => \@userlist ), submit('&#931;&#973;&#957;&#948;&#949; +&#963;&#951;!')); print end_form; my $userlogin = param('userlogin'); if ( param('&#931;&#973;&#957;&#948;&#949;&#963;&#951;!') ) { unless( grep { $_ eq $userlogin } @userlist ) + #Check if userlogin name exists in the drop down menu { print br() x 2, h1( {class=>'big'}, "&#916;&#949;&#957; &#965; +&#960;&#940;&#961;&#967;&#949;&#953; &#967;&#961;&#942;&#963;&#964;&# +951;&#962; &#956;&#949; &#972;&#957;&#959;&#956;&#945;: $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 = "&#922;&#945;&#955;&#974;&#962; &#942;&#955;&#952;&#94 +9;&#962; $userlogin! &#935;&#945;&#943;&#961;&#959;&#956;&#945;&#953; + &#960;&#959;&#965; &#946;&#961;&#943;&#963;&#954;&#949;&#953;&#962; +&#964;&#951;&#957; &#963;&#949;&#955;&#943;&#948;&#945; &#949;&#957;& +#948;&#953;&#945;&#966;&#941;&#961;&#959;&#965;&#963;&#945;. &#932;&#949;&#955;&#949;&#965;&#964;&#945;&#943;&#945 +; &#966;&#959;&#961;&#940; &#942;&#961;&#952;&#949;&#962; &#949;&#948 +;&#974; &#969;&#962; $row->{host} &#963;&#964;&#953;&#962; $row->{dat +e} ! &#928;&#961;&#959;&#951;&#947;&#959;&#973;&#956;&#949 +;&#957;&#959;&#962; &#945;&#961;&#953;&#952;&#956;&#974;&#957; &#949; +&#960;&#953;&#963;&#954;&#941;&#968;&#949;&#969;&#957; => $row->{coun +ter} &#932;&#943; &#952;&#945; &#960;&#940;&#961;&#949;&#9 +53;&#962; &#963;&#942;&#956;&#949;&#961;&#945; !?"; $select = $db->prepare( "UPDATE guestlog SET username=?, count +er=counter+1, date=? WHERE host=?" ); $select->execute( $userlogin, $date, $host ); } } else { if( $host eq "&#925;&#943;&#954;&#959;&#962;" ) { $data = "&#915;&#949;&#953;&#940; &#963;&#959;&#965; &#925;&#9 +53;&#954;&#972;&#955;&#945;! &#928;&#974;&#962; &#960;&#940;&#957;&#9 +49; &#964;&#945; &#954;&#941;&#966;&#953;&#945;? ;-)"; } else { $data = "&#915;&#949;&#953;&#940; &#963;&#959;&#965; $host! &#904;&#961;&#967;&#949;&#963;&#945;&#953; &#947 +;&#953;&#945; 1&#951; &#966;&#959;&#961;&#940; &#949;&#948;&#974; !! &#917;&#955;&#960;&#943;&#950;&#969; &#957;&#945; &#946 +;&#961;&#949;&#943;&#962; &#972;&#955;&#959; &#964;&#959; &#955;&#959 +;&#947;&#953;&#963;&#956;&#953;&#954;&#972; &#960;&#959;&#965; &#949; +&#960;&#953;&#952;&#965;&#956;&#949;&#943;&#962; :-)"; } unless( $host eq "&#925;&#943;&#954;&#959;&#962;" ) { $select = $db->prepare( "INSERT INTO guestlog (username, count +er, host, date) VALUES (?, ?, ?, ?)" ); $select->execute( 'Guest', 1, $host, $date); } }
      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}
      A reply falls below the community's threshold of quality. You may see it by logging in.