Separate Javascipt code from pure Perl code

Discussion in 'Perl Misc' started by Nikos, May 7, 2007.

  1. Nikos

    Nikos Guest

    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.selected) {
    var item = popup.options.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.selected) {
    olist.options = null;
    }
    }
    olist.selectedIndex = -1;
    }
    function selectAll() {
    var olist = document.getElementById('ordered_items');
    for (var i = 0; i<olist.options.length; i++) {
    olist.options.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
    #===============================================================================
     
    Nikos, May 7, 2007
    #1
    1. Advertising

  2. 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...
     
    Pertti Kosunen, May 7, 2007
    #2
    1. Advertising

  3. Nikos

    Nikos Guest

    On May 7, 2:06 pm, Pertti Kosunen <> 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....
     
    Nikos, May 7, 2007
    #3
  4. 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.
     
    Pertti Kosunen, May 7, 2007
    #4
  5. Nikos

    Nikos Guest

    On May 7, 2:06 pm, Pertti Kosunen <> 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?
     
    Nikos, May 7, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EvgueniB
    Replies:
    1
    Views:
    633
    Anthony Borla
    Dec 15, 2003
  2. Todd Aspeotis
    Replies:
    3
    Views:
    466
    Kanenas
    May 30, 2005
  3. Frank Fredstone
    Replies:
    1
    Views:
    443
    Jean-Francois Briere
    Jun 27, 2006
  4. Replies:
    4
    Views:
    796
    Ben C
    Mar 29, 2008
  5. Ronald Celis

    Javascipt Menus and DropDownList

    Ronald Celis, Dec 8, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    94
    Ronald Celis
    Dec 8, 2003
Loading...

Share This Page