Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9008501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:57:07+00:00 2026-06-16T01:57:07+00:00

I have two JSP pages that must use the same javascript. The script attaches

  • 0

I have two JSP pages that must use the same javascript. The script attaches a function on an anchor. This function will call a database operation through the controller and service layer when user click on the anchors.

Both JSP pages has these anchors. Hence, it will be good if I can reuse this script in both pages. I am planning to create a JSP page that only has this script and include this pages in both jsp pages. Is this a good practice in re-using a javascript ? Are there any other better ways to do this ?

This is a snippet of the script:

$(document).ready(function(){

         $('a[name*="like"]').click(function() {

             var hrefName = $(this).attr("name");
             var href = $(this);
             $.ajax({  
                  type: "POST",  
                  url: "likeARecipe",  
                  data: 'recipeId=' + $(this).attr("title") + '&operation=' + $(this).attr("name"),  
                  success: function() {  

                      if(hrefName == 'unlike')
                      {
                          $(href).attr("name","like");
                          $(href).text("like");
                      }else {
                          $(href).attr("name","unlike");
                          $(href).text("unlike");
                      } 
                  }  
                });  
                return false; 
         });



      });

UPDATE

I decided to put the script into a common.js script. I place this script under scripts/common.js.

I used tag to render the URL to this script.

<spring:url value="/resources/scripts/common.js" var="common_js" />
<script src="${common_js}" type="text/javascript"><jsp:text/></script>

I configure spring to read this script by specfying these resources in a context file:

<resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

However, spring did not load the script in the JSP Pages. Any suggestion on a way to trouble shot the problem ?

UPDATE

I found a solution to this problem. I have to modify the script. I enclosed the script inside a function():

(function(){
    alert("test");
    $(document).ready(function(){

         $('a[name*="like"]').click(function() {


             var hrefName = $(this).attr("name");
             var href = $(this);
             $.ajax({  
                  type: "POST",  
                  url: "likeARecipe",  
                  data: 'recipeId=' + $(this).attr("title") + '&operation=' + $(this).attr("name"),  
                  success: function() {  

                      if(hrefName == 'unlike')
                      {
                          $(href).attr("name","like");
                          $(href).text("like");
                      }else {
                          $(href).attr("name","unlike");
                          $(href).text("unlike");
                      } 
                  }  
                });  
                return false; 
         });
      });

})(jQuery);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:57:09+00:00Added an answer on June 16, 2026 at 1:57 am

    Create an external .js file and reference it from both JSP pages, like this:

    <script src="displaydate.js" type="text/javascript"></script>
    

    Take a look here: http://www.javascriptkit.com/javatutors/external.shtml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two JSP pages that can access a Servlet. Now, I want when
I have two JSP pages that are just about identical. Each page has an
Problem: I have two identical pages home.jsp and contact.jsp with same structure. They differs
I have this strange behavior in my JSP page: I have two Integer variables
I have two objects: Form . A form that I populate from a JSP
I'm using Struts2. I have two web forms that have the same code. I
I have two jsp pages and one servlet. When user clicks submit button then
I have two problems. The first one is that I'm using JSP and that
I have two jsp pages. one.jsp and two.jsp . On one.jsp there is a
I have a page that will display one of two options for a Struts2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.