-1

I want to add jquery datepicker in cgi.pm file and want to add the script jquery tag inline for datepicker as it is not working in the external javascript file. how can this be added.. I have to use cgi.pm because I am told to do so at my workplace, not by choice...

The code in ivalid.js and sthome.css is working perfectly fine...

This is the function I want to add internally in the cgi file.

$( function() {
    $( "#datepick" ).datepicker({ minDate: 0, maxDate: "+1M +10D" });
  } );

cgi.pm

$q->start_html
(
        -title=>'ai',
    -style=>[{'src'=>'/sthome.css'},
         {'src'=>'//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css'},
         {'src'=>'/resources/demos/style.css'}],
     -script=>[{-language=>'javascript',
              -src=>'/ivalid.js'},
              {-language=>'javascript',
             -src=>'https://code.jquery.com/jquery-1.12.4.js'},
             {-language=>'javascript',
            -src=>'https://code.jquery.com/ui/1.12.1/jquery-ui.js'}
              ]
),
Sanjay
  • 29
  • 1
  • 8
  • Could you please clarify what your problem is? Any errors? – Kosh Feb 11 '18 at 18:42
  • @KoshVery.. the error is : Uncaught ReferenceError: $ is not defined at inputvalidation.js:17 – Sanjay Feb 11 '18 at 18:56
  • Don't see `inputvalidation.js` in your code. Where is it being included? – Kosh Feb 11 '18 at 20:00
  • 1
    @Sanjay, To the left of the answer that solved your problem, there is a checkmark. If you click it, then it does three things: 1) It tells everyone else that your question was answered to your satisfaction, so they don't have to bother reading the question. 2) It awards points to the person who answered your question. 3) It awards points to you as well. – 7stud Feb 11 '18 at 20:07

3 Answers3

2

If I understood your question correctly, all you need to do is include a string in the -script=>[...] array:

use warnings;
use strict;
use CGI;

my $q = CGI->new;

my $JAVASCRIPT = <<'ENDJS';
$( function() {
    $( "#datepick" ).datepicker({ minDate: 0, maxDate: "+1M +10D" });
} );
ENDJS

print $q->start_html(
    -title=>'Baseline Automation Input',
    -style=>[ {'src'=>'/sthome.css'}, ],
    -script=>[
        {-language=>'javascript', -src=>'/ivalid.js'},
        $JAVASCRIPT,
    ],
);

Output:

<!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-US">
<head>
<title>Baseline Automation Input</title>
<link rel="stylesheet" type="text/css" href="/sthome.css" />
<script src="/ivalid.js" type="text/javascript"></script>
<script type="text/javascript">//<![CDATA[
$( function() {
    $( "#datepick" ).datepicker({ minDate: 0, maxDate: "+1M +10D" });
} );

//]]></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
haukex
  • 2,973
  • 9
  • 21
  • my $q = CGI->new(); my $JAVASCRIPT = <<'ENDJS'; $( function() { $( "#datepick" ).datepicker({ minDate: 0, maxDate: "+1M +10D" }); } ); ENDJS print $q->header, $q->start_html ( -style=>[{'src'=>'/sthome.css'}, {'src'=>'//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css'}, {'src'=>'/resources/demos/style.css'}], -script=>[{-language=>'javascript', -src=>'/ivalid.js'}, {-language=>'javascript', -src=>'https://code.jquery.com/jquery-1.12.4.js'}, {-language=>'javascript', $JAVASCRIPT, ] ), tried this.. doesn't work – Sanjay Feb 11 '18 at 19:09
  • @Sanjay That's really difficult to read, but yes, it looks like what I meant. I just edited down my example to leave out a few elements to make it shorter. – haukex Feb 11 '18 at 19:14
  • its showing after compiling as you said.. if i execute like this in normal html file then the datepicker is showing.. but in cgi.pm format its not showing – Sanjay Feb 11 '18 at 19:17
  • what is this cdata? – Sanjay Feb 11 '18 at 19:18
  • @Sanjay Sorry, I don't understand what you mean with *"in cgi.pm format its not showing"* - not showing where and how? As for `CDATA`, see e.g. [When is a CDATA section necessary within a script tag?](https://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag) – haukex Feb 11 '18 at 19:21
  • when i compile the script, the result is the same as you just showed(in html format), but the date datepicker is not working. and there are no errors also. – Sanjay Feb 11 '18 at 19:29
  • 1
    ya it works now... i was missing the id tag actually – Sanjay Feb 11 '18 at 19:36
  • 1
    Nice work! One missing comma in my `$q->textfield()` element caused the datepicker to not get installed because the attributes for the `` were all screwed up--most importantly the id attribute. I always forget about `$ perl cgi.pl`, which will show the output of the cgi program. Your answer with the output jogged my memory, which allowed me to fix my html. My question: How in the heck did you come up with that syntax in the `-script` array? According to the CGI.pm docs, the syntax should be `{-code=>$JSCRIPT}`. Of course, the output shows that your syntax also works. – 7stud Feb 11 '18 at 20:00
  • @7stud Thanks, and I have to admit I don't remember where I originally got that syntax from - for this post I pulled some code out of an existing, older CGI script I had lying around. It's a pure guess, but maybe back then I looked into the docs, saw the first example of `-script => $JSCRIPT` and then the mention that you can put multiple scripts in an arrayref... or maybe I got it from someone else's code online :-) – haukex Feb 11 '18 at 20:35
  • No worries. Thanks for getting back to me. – 7stud Feb 11 '18 at 21:21
1

Set ivalid.js after the jquery and jquery-ui:

$q->start_html
(
    -title=>'Baseline Automation Input',
    -style=>[
        {'src'=>'/sthome.css'},
        {'src'=>'//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css'},
        {'src'=>'/resources/demos/style.css'}
    ],
    -script=>[
        {-language=>'javascript', -src=>'https://code.jquery.com/jquery-1.12.4.js'},
        {-language=>'javascript', -src=>'https://code.jquery.com/ui/1.12.1/jquery-ui.js'}
        {-language=>'javascript', -src=>'/ivalid.js'},
    ]
), 
Kosh
  • 16,966
  • 2
  • 19
  • 34
1

I also got your jquery to work by putting the jquery inline in the file--so there is no reason that the jquery can't be put in an external file as well. I think you must have a path problem to your external js file.

I use apache for my server, and here is my directory structure:

apache2/
    cgi-bin/
        perl4.pl
    htdocs/
       page.html
       js/   
           datepicker_installer.js

I can request perl4.pl with this url:

http://localhost:8080/cgi-bin/perl4.pl

My apache server is configured to listen on port 8080.

I can request pages in the htdocs directory like this:

http://localhost:8080/page.html

Note how I don't have to specify the htdocs directory in the url. At first glance, a relative path from the cgi-bin directory to the htdocs/js directory looks like it would be:

../htdocs/js/datepicker_installer.js

But, in fact my browser will fail to load the js file with that path:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/htdocs/js/datepicker_installer.js

The correct relative path does not include the htdocs directory:

../js/datepicker_installer.js

With that path, I can put your js in the external file datepicker_installer.js, and here is what the perl cgi looks like:

#!/usr/bin/env perl

use strict;
use warnings;
use CGI qw(:all); 
use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;

my $JSCRIPT = q-

$( function() {
    $( "#datepick" ).datepicker({ 
        minDate: 0, maxDate: "+1M +10D" 
    });
} )

-;

print(

    $q->header,

$q->start_html(
    -title=>'Baseline Automation Input',
    -style=>[
        {-src=>'https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css'}
    ],
    -script=>[
        {-src=>'http://code.jquery.com/jquery-3.3.1.js',
         -integrity=>'sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=',
         -crossorigin=>'anonymous'
        },
        {-src=>'https://code.jquery.com/ui/1.12.1/jquery-ui.js',
         -integrity=>'sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=',
         -crossorigin=>'anonymous'
        },
        #{-code=>$JSCRIPT}  #This works too!
        {-src=>'../js/datepicker_installer.js'}  #<===HERE
    ],

),

    $q->div(
        {-id=>"divtop"},
        $q->p("Here's a paragraph")
    ),

    $q->start_form(
        -method=>'post'
    ),

    $q->div(
        "Reservation Date:",
        $q->textfield(
            -name=>'reservation',  #<==Don't forget a comma here!
            -size=>50,
            -maxlength=>80,
            -id=>'datepick'
        )
    ),

    $q->end_form(),
    $q->end_html()

);

datepicker_installer.js:

$( function() {
    $( "#datepick" ).datepicker({ 
        minDate: 0, maxDate: "+1M +10D" 
    });
} );

And here is the output produced by that script:

/usr/local/apache2/cgi-bin$ perl perl4.pl

Content-Type: text/html; charset=ISO-8859-1

<!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-US">
<head>
<title>Baseline Automation Input</title>
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-3.3.1.js" type="text/javascript"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" type="text/javascript"></script>
<script src="../js/datepicker_installer.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div id="divtop"><p>Here's a paragraph</p></div><form method="post" action="http://localhost" enctype="multipart/form-data"><div>Reservation Date: <input type="text" name="reservation"  size="50" maxlength="80" id="datepick" /></div></form>
</body>
</html>

Note in the output that CGI.pm does not accept arbitrary attributes for the -script hashes. Therefore, you cannot follow best practices by including the integrity and crossorigin attributes.

7stud
  • 46,922
  • 14
  • 101
  • 127