Nik has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Separate Javascipt code from pure Perl code
by marto (Cardinal) on May 07, 2007 at 13:49 UTC
    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
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Separate Javascript code from pure Perl code
by blazar (Canon) on May 07, 2007 at 13:50 UTC
    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.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Separate Javascipt code from pure Perl code
by shmem (Chancellor) on May 07, 2007 at 17:52 UTC
    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}
      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.