This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSP and the fly likes Calling javascript function in js file from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Calling javascript function in js file from JSP" Watch "Calling javascript function in js file from JSP" New topic
Author

Calling javascript function in js file from JSP

Mil Mnj
Greenhorn

Joined: Dec 27, 2008
Posts: 5
Hi All,

I am trying to call javascript function from jsp file

Following is JSP code :

<script type ="text/javascript" src = "/staic/javascript/Jsrp.js" >
.
.
<a href = javascript:jsCalled()> Call JS Function </a>; </br>


and following js code in Jsrp.js file located in

function jsCalled()
{
alert('jsCalled function called');
}

but when I click (Call JS Function) I get js error as jsCalled is not defined.

Can any one help?
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
In src attribute of script tag give whole path of file(including context path).
by this it will work(i thing so)


Thanks,
Shailesh
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Do you understand how relative URL's work?
Originally posted by Milind Manjrekar:
<script type ="text/javascript" src = "/staic/javascript/Jsrp.js" >

Relative URL's are relative to the current context.
The leading slash will bring you to the domain root.

Verify if it is right. Also, check for any typos, "staic" isn't a valid English word.
Mil Mnj
Greenhorn

Joined: Dec 27, 2008
Posts: 5
it was typo error while posting here

Correct code is:
<script type ="text/javascript" src = "/static/javascript/Jsrp.js" >

I tried removing leading slash. However, I still get the same error jsCalled() is not defined.

I think this error needs setting context path, not sure though.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Originally posted by Milind Manjrekar:
it was typo error while posting here

Typo? Why didn't you just copypaste it? Changing code because of posting it at a forum is a bad idea.

Correct code is:
<script type ="text/javascript" src = "/static/javascript/Jsrp.js" >

I tried removing leading slash. However, I still get the same error jsCalled() is not defined.

Tried? So you don't understand how relative URL's work?
Anyway, what's the absolute URL with which you requested the JSP file? (that URL which you see in the browser address bar). Also, what is the absolute URL with which you can call the .js file independently? Once you know that, you should be able to extract the right relative URL of the .js file from it.

I think this error needs setting context path, not sure though.

This sentence makes no sense.
Mil Mnj
Greenhorn

Joined: Dec 27, 2008
Posts: 5
ya, typo error while posting here, not a big issue , I dont know how t got missing , anyway I know how relative URLs work, and have resolved the issue.

Thanks
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
What was the problem then?
Mil Mnj
Greenhorn

Joined: Dec 27, 2008
Posts: 5
ya, typo error while posting here, not a big issue , I don't know how t got missing , anyway I know how relative URLs work, and have resolved the issue.

Problem was some other JSP comment was giving problem for JS function execution. Now, please don't ask why did you type JSP comment in JS file(because I wanted to test the same).
Anyway, thanks for your help

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66

Typos are a big deal. If you waste people's time following "red herrings" they will soon learn to ignore your questions. Please read this for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

first, try to call local javascript function (javascript function written in the same page);



this is used just for ensure that the javascript function could be called in JSP pages..
and then try to move that function to certain javascript file..
and write your path of file correctly..

Please correct me if i'm wrong...


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
Mil Mnj said 'Problem was some other JSP comment was giving problem for JS function execution'.
Could someone explain this to me ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66

I have no idea what was meant by that.

Is there a specific problem you are having? If so, start a new topic describing your issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling javascript function in js file from JSP