CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Add Javascript files and Javascript code using CGI.pm

 



babyboy
New User

Jul 2, 2008, 4:08 PM

Post #1 of 8 (1246 views)
Add Javascript files and Javascript code using CGI.pm Can't Post

It's a beautiful rocking situation that I am facing over here. Here it goes:

I have 2 external Javascript files: main1.js and main2.js

I have some Javascript code stored in a Perl variable $javascript

Now I want to add these 2 external Javascript files and also the Javascript code stored in the Perl variable to the page that I output. But I want this done using CGI.pm

Can anyone please help me by submitting the code?


KevinR
Veteran


Jul 2, 2008, 11:38 PM

Post #2 of 8 (1241 views)
Re: [babyboy] Add Javascript files and Javascript code using CGI.pm [In reply to] Can't Post

I am 90% sure this topic is covered in the CGI modules documentation.

http://perldoc.perl.org/CGI.html
-------------------------------------------------


babyboy
New User

Jul 3, 2008, 9:47 AM

Post #3 of 8 (1235 views)
Re: [KevinR] Add Javascript files and Javascript code using CGI.pm [In reply to] Can't Post

Can you please quote the text to me cos I've gone through the manual time after time and could not find it. It would greatly help me. Thanks in advance!


KevinR
Veteran


Jul 3, 2008, 2:12 PM

Post #4 of 8 (1228 views)
Re: [babyboy] Add Javascript files and Javascript code using CGI.pm [In reply to] Can't Post

I am not sure how you could miss it but, at the top of the documentation You will see a heading:

GENERATING DYNAMIC DOCUMENTS

under that heading is a sub heading:

CREATING THE HTML DOCUMENT HEADER

in that section you will scroll down a little until you see:

JAVASCRIPTING:

there are some examples and discussion of how to include embedded and external javascript code, here is one example for importing javascript from a file:


Code
 print $q->start_html(-title=>'The Riddle of the Sphinx', 
-script=>{-type=>'JAVASCRIPT',
-src=>'/javascript/sphinx.js'}
);


http://perldoc.perl.org/CGI.html#CREATING-THE-HTML-DOCUMENT-HEADER
-------------------------------------------------


babyboy
New User

Jul 3, 2008, 3:37 PM

Post #5 of 8 (1223 views)
Re: [KevinR] Add Javascript files and Javascript code using CGI.pm [In reply to] Can't Post

I have used the coding technique shown there but I couldn't do what I wanted to. Ok... let me show you what I want coded into the CGI page.


Code
my $javascript = <<END; 
function checkRemove(element) {
var obj = document.getElementById(element);
if (obj) {
obj.getParentNode().removeChild(obj);
return true;
}
return false;
}
END

my $srcfile = './wanker.js';

print header,
start_html(-title=>"Wanker!",
-script=>{-type=>"text/javascript",
-src=>"$srcfile"},
$javascript
);



I couldn't make the above piece of code to work. Any suggestions how to start it (or kick start it)??


KevinR
Veteran


Jul 4, 2008, 11:12 AM

Post #6 of 8 (1211 views)
Re: [babyboy] Add Javascript files and Javascript code using CGI.pm [In reply to] Can't Post

"Can't make it work" is too vague. What happens? Does the script not find the javascript file? Your syntax to include the embeded javascript code does not look correct.

To include javascript in the head section that you define in the script:


Code
my $javascript = <<END;  
function checkRemove(element) {
var obj = document.getElementById(element);
if (obj) {
obj.getParentNode().removeChild(obj);
return true;
}
return false;
}
END


print header,
start_html(-title=>"Wanker!",
-script=>$javascript
);


To import it from a file:


Code
  
my $srcfile = './wanker.js';

print header,
start_html(-title=>"Wanker!",
-script=>{-type=>"text/javascript",
-src=>$srcfile},
);


the source of the file is a URL relative to where the cgi script is. If the script is in the cgi-bin the wanker.js file would be in the folder just above the cgi-bin, which is probably the public_html (or www) folder. I would name a folder 'javascript' in the public_html folder and put the file in there and use:

my $srcfile = 'javascript/wanker.js';

After your CGI script runs check the source code of the html document it outputs and see what gets printed.
-------------------------------------------------


babyboy
New User

Jul 4, 2008, 3:01 PM

Post #7 of 8 (1206 views)
Re: [KevinR] Add Javascript files and Javascript code using CGI.pm [In reply to] Can't Post

What if wanted to do both? Import from a source file and include javascript in the head section that I define in the $javascript variable.

Now do you understand the situation I'm facing cos while using CGI.pm's start_html(), I can call this function only once while creating a page. So that does not give me the chance to write two separate lines of code containing calls to this start_html() function, for each type of script inclusion as you had suggested.


KevinR
Veteran


Jul 4, 2008, 9:45 PM

Post #8 of 8 (1202 views)
Re: [babyboy] Add Javascript files and Javascript code using CGI.pm [In reply to] Can't Post

try this:


Code
use CGI qw/:standard/; 
my $javascript = <<END;
function checkRemove(element) {
var obj = document.getElementById(element);
if (obj) {
obj.getParentNode().removeChild(obj);
return true;
}
return false;
}
END

my $srcfile = './wanker.js';

print header,
start_html(-title=>"Wanker!",
-script=>{-type=>"text/javascript",
-src=>"$srcfile"},
-head=>script{-type=>"text/javascript"},$javascript,
);


the -head tag allows for arbitary things to placed in the head section of the html document. It is discussed in the same section I referenced previously.
-------------------------------------------------


(This post was edited by KevinR on Jul 4, 2008, 9:49 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives