0

This should be simple - don't get what I'm doing wrong! This is a very basic test (I'm new to PERL and Javascript) - this is the CGI file:

#! /usr/local/bin/perl

print "Content-type: text/html\n\n";
print "<html>\n" ;
print "<head>Hello\n";
print '<script type="text/javascript" language="javascript" src="wibble.js">\n';
print "</script>\n";
print "</head>\n";

print "<body>\n";

$fred = "Fred";
$numb = 7;

print <<TEST;

<p>Starting...</p>
<p><script type="text/javascript" language="javascript">
theText = "$fred";
theNum = "$numb";
document.writeln("Direct write...");
document.writeln("Number is: " + theNum);
document.writeln("Text is: " + theText);

testWrite(theNum, theText);

</script></p>

<p>...ending JS</p>

TEST

and in wibble.js:

function testWrite(num1, txt1)
{
   document.writeln("In testWrite...");
   document.writeln("Number is: " + num1);
   document.writeln("Text is: " + txt1);
}

In my browser, I get the first set of writeln's but my function is never called. The error on the webpage says 'Object expected' at line 15 (the 'print <<TEST' line).

I mostly suspect I haven't got the right path in my src element but I've tried every combination I can think of ('.', './', full path etc) - nothing works. The js file is in the same dir as the CGI file.

(I actually originally had the function call with no parameters, hoping that theNum and theText are global and will still work (that was the original point of this test program)).

Please put me out of my misery...

As requested, here is source code from browser:

<html>
<head><script type="text/javascript" language="javascript" src="wibble.js"></script>
</head>
<body>

<p>Starting...</p>
<p><script type="text/javascript" language="javascript"> 
theText = "Fred";
theNum = "7";
document.writeln("Direct write...");
document.writeln("Number is: " + theNum);
document.writeln("Text is: " + theText);

testWrite(theNum, theText);

</script></p>

<p>...ending JS</p>

</body>
</html>

and this is the actual output on the web page:

Starting...

Direct write... Number is: 7 Text is: Fred 

...ending JS
5
  • Single-quoted strings don't understand \n as a newline. Check the output of your script. I except you'll see a literal \n in the middle of your <script> tag. Commented Jan 6, 2012 at 12:53
  • You should be examining your web page as it is in your browser, and showing us that markup if you want our help. Commented Jan 6, 2012 at 13:00
  • Added html (and tidied up bits mentioned by mkb and Marc B - thanks!) Commented Jan 6, 2012 at 13:23
  • OK, sorted - I can't answer my own question until later but basically, it was just a matter of putting my js file in the right dir and fiddling with the src element until it found it. Thanks for all your help. Commented Jan 6, 2012 at 14:09
  • 1
    The title is very confusing: You're not calling javascript from perl, you're generating html & javascript using perl. Commented Jan 6, 2012 at 14:33

1 Answer 1

3

Did you check your server's log to see if wibble.js is ever requested? If it's not, then there's your problem. As well, while not really the problem, this line:

print "<head>Hello\n";

is generating bad html. You can't have "bare" text in the <head> block.

For global JS variables, you use the var keyword.

x = 7; // local
var y = 7; // global
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry to be thick but which server log should I be lloking for?
To see if you are accessing wibble.js you would look in the access log.
To be clear, I work in a large company, it's not my webserver and I'm not an SA. I'm on the Unix server where my files are but I don't know if that is the webserver or not (sorry) - any way of telling? Any standard dirs I could look in?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.