Forum Moderators: open

Message Too Old, No Replies

Questions on including external Javascript

         

ericazhj

1:24 am on Feb 24, 2007 (gmt 0)

10+ Year Member



Hi,
I want to include an external Javascript in my html head. However, it could not work. The html is under "C:/Program Files/Apache Group/Apache2/htdocs/". And I also placed clienthint.js under the same directory. I have tried and get that: if I embed the script in the html, then it could work correctly. If I include it as an external Javascript, like above, it could not work. Also, I have tried IE and the same thing happen. Could anybody help me figure out the problem or give me some suggestions? Thanks.

The html is:

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

<form>
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>

<p>Suggestions: <span id="txtHint"></span></p>

</body>
</html>

In this script, showHint(this.value) is a function defined in clienthint.js.

The Javascript is listed below:
var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="gethint.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

phranque

2:07 am on Feb 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



does your browser have a javascript console that offers any hints?
it might be the missing semicolon at the end of the first line.

ericazhj

2:25 am on Feb 24, 2007 (gmt 0)

10+ Year Member



No. On mozilla, there is no information. On IE, they just said there is some error in your web page. I am not sure if my configuration for apache is correct. However, why when I use embedd Javascript it works fine?

ericazhj

2:40 am on Feb 24, 2007 (gmt 0)

10+ Year Member



I checked the logs of Apache:

In Errorlog file:
Sun Sep 10 18:37:36 2006] [notice] Parent: Received restart signal -- Restarting the server.
[Sun Sep 10 18:37:36 2006] [notice] Child 1384: Exit event signaled. Child process is ending.
[Sun Sep 10 18:37:36 2006] [notice] Apache/2.0.59 (Win32) configured -- resuming normal operations
[Sun Sep 10 18:37:36 2006] [notice] Server built: Jul 27 2006 15:55:03
[Sun Sep 10 18:37:36 2006] [notice] Parent: Created child process 3344
[Sun Sep 10 18:37:36 2006] [notice] Child 3344: Child process is running
[Sun Sep 10 18:37:37 2006] [notice] Child 1384: Released the start mutex
[Sun Sep 10 18:37:37 2006] [notice] Child 3344: Acquired the start mutex.
[Sun Sep 10 18:37:37 2006] [notice] Child 3344: Starting 250 worker threads.
[Sun Sep 10 18:37:38 2006] [notice] Child 1384: Waiting for 250 worker threads to exit.
[Sun Sep 10 18:37:38 2006] [notice] Child 1384: All worker threads have exited.
[Sun Sep 10 18:37:38 2006] [notice] Child 1384: Child process is exiting
[Sun Sep 10 18:37:44 2006] [error] [client 128.189.138.12] C:/Program Files/Apache Group/Apache2/htdocs/clienthint.js is not executable; ensure interpreted scripts have "#!" first line, referer: [128.189.138.12...]
[Sun Sep 10 18:37:44 2006] [error] [client 128.189.138.12] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Group/Apache2/htdocs/clienthint.js, referer: [128.189.138.12...]

In accesslog file:
128.189.138.12 - - [10/Sep/2006:18:37:44 -0700] "GET / HTTP/1.1" 304 -
128.189.138.12 - - [10/Sep/2006:18:37:44 -0700] "GET /clienthint.js HTTP/1.1" 500 617

I hope this kind of information could help.

phranque

2:43 am on Feb 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



what happens when you get the included javascript file directly?
http://www.example.com/clienthint.js

will apache serve it to your browser?

what happens if you specify a fully qualified url for the included javascript file?

<script type="text/javascript" src="http://www.example.com/clienthint.js" ></script>

phranque

2:55 am on Feb 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



do you have an AddHandler directive specified in your apache config?

ericazhj

2:59 am on Feb 24, 2007 (gmt 0)

10+ Year Member



what happens when you get the included javascript file directly?
http://www.example.com/clienthint.js

I tried it. It retured "The server encountered an internal error or misconfiguration and was unable to complete your request."

You know, I also tried http://www.example.com/phptest.php. And it works.

will apache serve it to your browser?
Yes.

what happens if you specify a fully qualified url for the included javascript file?
<script type="text/javascript" src="http://www.example.com/clienthint.js" ></script>

I tried it also could not work. Here I placed both phptest.php and clienthint.js under htdocs directory of Apache HTTP server.

ericazhj

3:04 am on Feb 24, 2007 (gmt 0)

10+ Year Member



Yes, my handlers include:

AddHandler cgi-script .cgi .js .pl
AddHandler type-map var

phranque

3:10 am on Feb 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



that's what i thought - try this:

AddHandler cgi-script .cgi .pl

you are telling apache to treat any.js file as a cgi script, which means the first line must be the path the the script processor such as:
#!/usr/bin/perl

you don't want a handler for javascript files as you are using them.

ericazhj

3:21 am on Feb 24, 2007 (gmt 0)

10+ Year Member



You are correct. Now it could work. Here I have another question, could I use Javascript both ways: cgi and included file?

Thanks a lot

phranque

5:40 am on Feb 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i have no experience with server side javascript other than querying the subject in a search engine.