Including Javascript code from a JSP file - javascript

I have a JSP file that gives me a bunch of JS code that I need to put in my view page (particularly in my JSP file that is the view for the current URL. Using Spring MVC) . Is there a way for it?
I have already tried a bunch of suggestions from here: Including JSP code from another JSP file correctly and here https://coderanch.com/t/596663/frameworks/Calling-jsp-include-tag-javascript but none gives clear answer on it. When the view is rendered after hitting the controller it just gave a plain JS code in the window instead of embedding it as JS code.
<html>
<head> </head>
<script type="text/javascript">
<jsp:include page="./myJSPfiles/MyJSPfileforScript" />
</script>
</html>
As I said, it just gives plain JS code in the browser instead of embedding it. I am fairly new to JSPs in general. Any suggestions?

Use something like below line of code.
<script type="text/javascript" src="${pageContext.request.contextPath}/js/index.js"></script>

Related

How add Javascript in GVnix/Spring roo project

I'm developing a web application and I want add a js file in my project to use it. I tried to add in this way:
<spring:url value="/resources/scripts/gestion.js" var="gestion"/>
<script src="${gestion}" type="text/javascript"></script>
The js loads correctly, but the problem is that the page is not showing anything, if I put this code in the end of the jsp file, before of tag <div> the jquery and another js file of the project not work although I can see the content of the jsp.
Somebody can help me with this?
You should include it into src/main/webapp/WEB-INF/tags/util/load-scripts.tagx file.
This tag is used by src/main/webapp/WEB-INF/layouts/default.jspx which is used by Apache Tiles configuration file src/main/webapp/WEB-INF/layouts/layouts.xml to generate the final html output.
Good luck!

JS file not triggering if I include another JSP file inside JSP file

I have a main jsp file, inside of that JSP file, I have invoke a JS file and included another JSP file called login.jsp.
Before adding the include for login.jsp the functions of my JS file can be called. After adding the login.jsp it is no longer working.
I tested invoking the JS file inside the login.jsp, it is working that way. The thing is, I cannot do that because many JSP pages will include login.jsp and I don't need to call each and every JS file that uses the login.jsp.
Here is the code:
this is my main jsp file:
<html>
<head>
.. some scriplets with no error ...
<SCRIPT type="text/javascript" src="<%=request.getContextPath()%>/js/pcLotsFrontProcessMaterialRegistration.js" ></SCRIPT>
.. other js files that is included this way ..
</head>
<body>
<div id="login_holder" align="center">
<jsp:include flush="true" page="../admin/login.jsp"></jsp:include>
</div>
... some codes ....
</body></html>
and my login.jsp is this :
<div id="login-box">
<table id="login-box-table">
... table contents ...
</table>
</div>
<script>
... functions ...
</script>
Is there something wrong with this format?
Please don't mind the scriplets and all I intend to modify them some other time, I just need to make this work first.
Thank you very much. :D
It's really hard to tell what's going on since you chose to omit the javascript, however it seems highly likely that something in the javascript in login.jsp is conflicting with the javascript in the 'main' jsp.
I suggest using a javascript console and debugger (like the Developer Tools in Chrome or FireFox) to find the problem.
By the way, this not a very good way to structure things, and is particularly prone to this type of problem.
First of all, you shouldn't have your javascript in your jsp pages - it should be in external .js files, and it should be name-spaced to avoid conflicts (I suggest reading Doug Crockford's 'Javascript - The Good Parts').
Second, there are much better ways to compose pages - for instance, take a look at the Apache Tiles project.
How about using JSP Scriptlets #include
like
<%#include file="../admin/login.jsp" %>
if your page is static page.

How to include a file in Javascript and Jsp?

Ok,i know in Php you can use include for a Php file...for example...
include("array.php");
Is there a way to do this in Javascript and Jsp?
From what i read i cant use for example include("array.javascript"); or include("array.jsp");
I want to load the array from a different file and i know how to do it with Php,but not with Javascript or Jsp. Any help?
I think you are a bit confused, JSP is an XML-based language executed at the server side that gets transformed into Java the first time is processed by the servlet container (server) whilst JavaScript is an interpreted language executed by the user's browser, not the server. PHP is also executed at the server-side and that include function has nothing to do with JavaScript.
Now, to port that behaviour to JSP you would use a JSP tag, more specifically, the <jsp:include /> tag:
<jsp:include page="array.jsp" />
But if what you are trying to is to link your JSP with a JavaScript file then you should use a better option which consist of using the standard HTML <script> tag:
<script type="text/javascript" src="array.js" />

Calling a javascript on an external page as a URL

There is a javascript on an external webpage that downloads a file. Can I pass a URL to a local script to download this automatically? Simply using http://webpage.com/javascript:theScript() doesn't seem to work, even when I type it into a browser. I'm very much novice in this space, be gentle! Thanks in advance.
This is too little info to tell you exactly how to do it (or even if it can be done at all). But what I can tell you:
http://webpage.com/javascript:theScript() cannot work the way you expect. The URL needs to locate a file, so if webpage.com was hosting a file named exactly `javascript:theScript()', then your browser would download the contents of this file and display it.
If the function you want to call resides in a javascript file, lets say, functions.js (URL: http://webpage.com/functions.js), then you can include this file in your own webpage and call functions from there.
This is where the special javascript: notation comes to place: It's a workaround to allow you to specify a javascript function in a regular <a href...> tag.
File: functions.js
function theFunction() {
alert ("Hey there!");
}
File: yourpage.html
<html>
<head>
<script type="text/javascript" src="http://webpage.com/functions.js">
</script>
</head>
<body onload="theFunction();">
Your text<br>
Click here
</body>
</html>
If this doesn't help you out, then you should post some code (ie. contents of the file containing the function to call, how you want to include the function in your page etc).
To include a script:
<script type="text/javascript" src="http://www.example.com/script.js"></script>
To execute code from the script you'll need a separate script tag:
<script type="text/javascript">
theScript(); //where theScript is a function defined in script.js
</script>
You should look at some tutorials for JavaScript. I recommend w3schools.

Can a single javascript file be used by multiple html files?

I have a javascript file main.js and five html files 1.html,2.html,3.html,4.html,5.html
I want to access the javascript file main.as in all files .
I have used in all of the five but I'm not able to access it . Is it possible to share the .js file among the html files.
Plz reply,
Thanks in advance
Prashant Dubey
Yes, it is entirely possible. That's the point of being able to have a JS file instead of having to embed all the code in the HTML.
yes this is well possible. You merely have to include
<script type="text/javascript" src="main.js"></script>
in your HTML files, preferably near the bottom (for faster rendering). What have you done so far?
Yes. Totally possible.
Just reference it in all of the files e.g. by
<script type="text/javascript" src="Scripts/main.js"></script>
Yes, it is possible.
Probably there is something wrong with the way you access the javascript from your html. Show us the <script ...>...</script> part of your html.
Yes. Are you using the correct path to the main.js file in your html files?
Create separate javascript file with .js extension with all your function in it and
just include this javascript file in the head tag of all the html scripts u wanna use that in.
Like::
<html>
<head>
<script type="text/javascript" src="JavaScriptFilePath.js"></script>
</head>
<body>
<!-- use javascript -->
It can happen both ways..
a single html file can use multiple javascript file
2.a javascript file can be used in several html files.
In first case javascript file load can be conditional based on location, user preferences, time, age group, content restriction.
You can see good example when facebook loads its page. I loads number of javascritps.

Categories