Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot call an external javascript

Status
Not open for further replies.

salmanelahi

Programmer
Oct 30, 2006
7
GB
Hi everyone

I am trying to call a very very simple external javascript in a jsp page. This script works fine when is in the jsp file but when i try to call it by
<script src=selectCss.js></script> or
<script src="${pageContext.request.contextPath}/docroot/samples/js/selectCss.js"></script>

it does not work. No doubt it is being accessed in the jsp file because if i put a statment like
document.write(navigator.appName) it shows the name of the browser on the page but do not set Css anyway.

this is the code for javascript (and there are no tags in the js file:) :
if(navigator.appName == "Netscape")
{
document.write('<link ="stylesheet" type="text/css" href=""${pageContext.request.contextPath}/docroot/samples/css/NS_homeRp.css"/>');
}
if(navigator.appName == "Microsoft Internet Explorer")
{
document.write('<link ="stylesheet" type="text/css" href=""${pageContext.request.contextPath}/docroot/samples/css/NS_homeRp.css"/>');
}

thanx a lot,

Salman
 
What does the HTML output show? The server-side code isn't important, it's the client-side HTML that we need to see.

Lee
 
it only shows the page without any formating as Css is not set and at the top of page it shows the name of browser if i include "document.write(navigator.appName)" this in the script as well.

and this is the source info of the output page:

<html><head><title>Riverfly Partnership</title><script type="text/javascript" src="/docroot/samples/js/selectCss.js"/><Script language="JavaScript">

 
You're missing a close script tag in the latest code you showed. The close script tag is in the first example, though.

If you didn't copy and paste directly from View-Source, please do it that way so we can see EXACTLY what the browser is working with.

Lee
 
If it's an external js file, I don't think it's processed server side, so the $ references won't be resolved.

Cheers,
Dian
 
No i did copied from View Source and there really is no End tag "</script>"...

But I think Dian is right... cuz I havent tried this. anyhow now i am out of office and dont have access to the code atm, will try dian's tip tomorrow.

but if you could think of anything else that could cause problem do let me know :)

thanx to both of you,
cheers,

Salman
 
Bud dian according to my understanding when we include an external js it is included as part of that page, isn't it. so it should be server side processed. I am very new to all this jsp and js world, so please can you guide me what actually happens when we use an external js using:

<script src="jscript.js"></script>

cheers,
Salman
 
You're making me doubt, but I think the jsp is compiled to an html, delivered to the browser and then it includes the javascript as a static resource, so it's not processed server-side.

Cheers,
Dian
 
That also is not the reason as I tried this very simple script its not working either. It works fine when within the jsp file but when I call it externally nothing happens.

both the jsp n js files are in same dir
following are the codes for jsp and view source out:

Jsp:
<jsp:root version="2.0" xmlns:jsp="xmlns:cms="urn:jsptld:cms-taglib"
xmlns:cmsu="urn:jsptld:cms-util-taglib"
xmlns:c="urn:jsptld:<jsp:directive.page contentType="text/html; charset=utf-8" />
<html>
<head>
<title>
<cms:eek:ut nodeDataName="title" />
</title>

<script src="myJs.js"> </script>

<!--<script language="JavaScript"> if (navigator.appName == 'Netscape'){alert('you are using firefox');}</script>-->

</head>
<body style="background-color:#fff" >

<h2>Hello World</h2>
</body>

</html>
</jsp:root>

View Source out Put:

<html><head><title>THis is the TITLE</title><script src="myJs.js"/></head><body style="background-color:#fff"><h2>Hello World</h2></body></html>

and this is the myJs.js file:

if (navigator.appName == 'Netscape'){alert('you are using firefox');}

now can anyone tell why this not working externally while its working fine within the file.... pleassssssssse

cheers

Salman
 
If you were to check your code with an HTML validator, you'll find that
Code:
<script src="myJs.js"/>
is not a valid way to close a script tag.

Have you viewed the page in Firefox and used their Javascript Console to find your error(s)?

Lee
 
thanx Dian you are right. When we call external javaScript we cannot use functions from JSP tag libraries as I was using ${pageContext.....
 
Have you tried naming your external script with the extension .jsp rather than .js?

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top