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 9111147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:26:13+00:00 2026-06-17T03:26:13+00:00

basically I have 2 problems with javascript and jsp file. First one concerns fact

  • 0

basically I have 2 problems with javascript and jsp file. First one concerns fact that I can’t include externall js file into jsp file. Basic structure of my project looks something like this:

system.web
       |
      WebContent
         |
         js
           |
            nowa_postac.js
         |
         jsp
           |
            nowa_postac.jsp

nowa_postac.js has only

 init = function() {
    test();
}
function test() {
        var myObject = JSON.parse('${gson}', null);
        alert(myObject.rasy.rasaList.length);
        alert(document.getElementById("testowy").id);
    }

I include javascript inside nowa_postac.jsp with:

<script type="text/javascript" src="${pageContext.request.contextPath}/js/nowa_postac.js"></script>

but I can’t call any function from nowa_postac.js in jsp file and

 src="<c:url value="/js/nowa_postac.js"

seems to be not working either…
test() returns nothing but alert(test) returns text, so javascript file is loaded…Also, everything works fine when test() is inside jsp file.

The second problem is about how to initialize function after the jsp page is loaded;

<body onload='init()'>

is not good here because in init() I need to refer to some objects created in jsp…
Thanks for any help

  • 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-17T03:26:15+00:00Added an answer on June 17, 2026 at 3:26 am

    I use jQuery to answer this question. First I have an external .txt file with the name includedScripts.txt which has the following format:

    NameOfFile~pathToJSFile
    

    For example:

    nowa_postac~nowaPostAc.js
    

    Note that nowaPostAc.js will be residing in a subfolder called Scripts.

    Next I load jQuery (I’m assuming you know how to do this, if not you can read up on http://www.learningjquery.com/2006/09/introducing-document-ready#more-5) and in my jQuery file I have the following:

    /*
        *
        * LOAD ADDITIONAL SCRIPTS
        */
        jQuery.ajax(
        {
            url: 'Scripts/includedScripts.txt'
            , timeout: 5000
            , success:
                function (data) {
                    myArray = data.split(/\n/);
                    for (var i = 0; i < myArray.length; i++) {
                        theContents = myArray[i].split("~");
                        goodToGo = false;
                        theFileName = "";
    
                        if (theContents.length == 1) {
                            goodToGo = true;
                            theFileName = theContents[0];
                        }
                        else {
                            for (var t = 0; t < theContents.length; t++) {
                                if (theContents[t].indexOf(".") != -1) {
                                    theFileName = theContents[t];
                                }
                                else if (location.pathname.indexOf(theContents[t]) != -1) {
                                    goodToGo = true;
                                }
                            }
                        }
                        if (goodToGo == true) {
                            pathToScript = (theFileName.indexOf("..") >= 0) ? theFileName.replace("..", "") : "Scripts/" + theFileName;
                            script = document.createElement('script');
                            script.type = 'text/javascript';
                            script.src = pathToScript;
                            if (jQuery("#ctl01").length > 0) {
                                jQuery("#ctl01").append(script);
                            }
                            else if (jQuery(".page").length > 0) {
                                jQuery(".page").append(script);
                            }
                        }
                    }
                }
            , error:
                function (xhr, textStatus, errorThrown) {
                    //alert("Could not load additional modules, please alert IT");
                    //make a log entry...
                }
            , async: false
        }); // load additional scripts
    

    You will more than likely need to edit the lines:

    if (jQuery("#ctl01").length > 0) {
                                jQuery("#ctl01").append(script);
                            }
                            else if (jQuery(".page").length > 0) {
                                jQuery(".page").append(script);
                            }
    

    to possibly read something like:

    if (jQuery("body").length > 0) {
                                jQuery("body").append(script);
                            }
                            else if (jQuery("SomeOtherDivOrClass").length > 0) {
                                jQuery("SomeOtherDivOrClass").append(script);
                            }
    

    What the script does (in conjunction with the includedScripts.txt file) is allow you to specify a different script to be loaded based on the page. This of course, assumes that you have a singular script that is loaded on all pages, for example:

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

    which would have the above code (Load Additional Scripts).

    This works for me, you’ll have to do some tweaking – let me know if you get through.

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

Sidebar

Related Questions

I have some localization problems in my webpage. There are basically two problems (that
I have a bit of JavaScript code that is specified in a configuration file
I have some problems with JavaScript timings..Basically i want it to start timing when
Basically I have this problem. I have an accordion that toggles one heading open
hey guys should be a easy one...I have some javascript that is turning my
I have a pretty simple problem. Basically I have an array called $list that
can anyone help? I have a problem aligning rounded corners in IE6/7. Basically everything
I have some problems with a PHP script that calls a Bash script.... in
I currently have a script written that begins downloading an large (1.3GB) XML file
I have a problem with the following javascript code. Basically I want to run

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.