Application URL in .js files - javascript

I am a beginner with spring and am trying to do ajax calls to refresh pages when button is clicked.If i deploy the application with different names in tomcat , will it be possible to pass the application context to the js file from jsp.
The jsp does ajax calls to a jquery methods in a script.js file to be reused.
the url in json method will return the result using requestmapping
Contents-scripts.js
$.getJSON('<app-url>/doSomething/getresult.htm', function(responseJson) {
$.each(responseJson, function(index, item) {
..
..
..
}
}
I have read about base tag to have the app-url as global , is there any other way to pass app-url to .js files either as parameter or can a EL call like ${pageContext.request.contextPath} be done from .js files

1) Passing the context :
Set the context of the page using JSP expression as described here -
How do you get the contextPath from JavaScript, the right way?
2) Refreshing the DOM - This is tricky - Are you refreshing some "part" of HTML or the complete page
Do you need to be answered ? This will entirely depend on the way you have implement your page

Related

how do i access server data in separate js file

aspx file
string firsrName="jafer";
myscript.js
GetMyName();
function GetMyName() {
alert('<%=firstName%>');
}
I am not getting my value
The line alert('<%=firstName%>'); use the Web Form Page syntax. It is actually not possible to get the value like this because this syntax cannot be used in external JS files.
The simpliest (but not cleanest) method is to write the JS method into the layout file or another aspx file.
Read How to get asp.net client id at external javascript file
You could make a global variable in your aspx page and access it in your js using window.objectName

How to open html file that links with images and stuffs in mvc

I have html files in another directory which links with some JavaScript, images and CSS. I want to open that html file in my site. I used the return File method in my controller action as shown below:
public ActionResult Index()
{
return File(Server.MapPath("~") + "index.html", "text/html");
}
But, it could not open the images and JavaScript that are linked to that in the .html file.
What is the proper solution? I'm using ASP.net MVC 4.
Thanks, I'm still a beginner at this, any help is greatly appreciated.
If i understand what you are trying to do here, you should not return an HTML file in the controller action but rather generate the view of the index page. If you are new to MVC you might benefit from reading more about Model-View-Controller pattern and when to apply it.
If all you are trying to do is serve a static HTML file, just let IIS serve that file and point the browser at the URL of that file directly. (i.e. Where it is stored on your web site).
If you want to have some logic dynamically linking to some static file, there are many ways to do it (both on the server and on the client), one of them would be to calculate the correct URL and redirect the user by returning a Redirect() operation from your controller action.

JQuery AJAX URL Local or Absolute

I have a portion of my web application that uses AJAX. I am using the JQuery AJAX call to add and remove items from the users shopping cart, and it works fine. The URL that I pass as a parameter in the AJAX call is the url-mapping defined in the web.xml file, minus the leading forward slash.
I tried to replicate this with another place in the application where I would like to use AJAX, but this time, the Servlet GET is not even being called at all. I have alerts in the javascript function, so I know the function is being called. Is there a way that I can see (within the ajax function call) what the fully qualified URL is that the GET request is being made to? Also, what is the 'rule' with respect to the AJAX URL? Is there a way that I can get to the context root? My Servlet is mapped to www.mydomain.com/contextRoot/un
I would rather not have to hardcode the context root into the function call, that way if it ever changes, I don't have to update all my javascript functions.
The "rule"
Ajax requests (or any http request from the page) that does not start with a / (or the full domain path) is relative to the current HTML file path.
How to view GET requests
Use the debugging tools in Chrome/Firefox or something like Fiddler2
How to get server root
If it is a simple www.host.com:
var root = location.protocol + '//' + location.host;
(Not sure if this works for context based domains).
You do not mention what server technology you are using, but if you are using MVC/Razor you can inject the server root into a Javascript variable with something like this in your master page header:
<script type="text/javascript>
window.domainRoot = "#(Url.Content("~/"))";
<script>
this injects a javaScript line like:
window.domainRoot = "http://www.mydomain/mycontext/";
you can then reference window.domainRoot from any JavaScript code.
If you are using PHP there will be an equivalent way to inject the server root.
A simple browser debugger (F12) and you can read the network traffic send.
From here it is possible to see all the details (sent and/or received)
I think The url plus parameter is contextRoot/un?para=*.

How to get colorbox request URL within colorbox content

I'm opening a colorbox within jsp as follows:
$.colorbox({maxHeight:"100%", href:'<c:url value="/html/dashboard_report.html" />?organization=${organizationKey}&category=${chartCategory}&severity=' + selection[0].row});
within my dashboard_report.html (which IS html, not JSP) using javascript I want to get the request so I can use the querystring to make further ajax calls to populate some data. window.location.href doesn't work because the window location hasn't changed.
Do I just store the applicable parameters in global vars when the colorbox is opened and read those in the colorbox content or is there some other way?
You can't use JSP to execute JS. Do you mean you're writing the JSP, and it's creating the script for you, then trying to execute?
In above case you can do something like:
$.colorbox.settings.maxHeight = "100%";
$.colorbox.settings.href = "http://someURL.com";
at the begning of your script and leverage them later dynamically, and those are only a couple of the available properties you can set to defaults... I'm not sure what you're trying to do can't accomplished without an nested AJAX call even though I'm opposed to doing that when possible.

render / call .aspx page html into another .aspx page

in my application, i have implemented ajax 4.0 client templates
currently my templates resides on same .aspx page. (say Main.aspx)
but i want to externalize them.(ie all the HTML would go on another page)
for that i have used $.get() like
$.get("/Module/getTemp/" + TemplateName, function(result) {...
now, i want getTemp function in Module to return the HTML (ie whatever that page contains) of the page having same name as Parameter 'TemplateName' has
into Main.aspx page (use c# in controller)
its like.. copy what other .aspx page contains and return it in calling (above)function from Main.aspx page
pls help
Have you tried using a partial view to return the html? You can setup a "templates" controller that serves up these templates. You can then have action methods for various templates. You would then be able to use routes like "/Templates/TemplateName" to fetch the html in your $.get call. If the template is only going to change once per page load, then I would be tempted to push parameters to my action method to be used in a view model.

Categories

Resources