Can Javascript get value from web.config AppSettings? - javascript

I have an aspx page, without the code behind cs file.
therefore, if I wanted to get the value from web.config AppSetting,
is it possible to do this in JavaScript or jQuery?
Appreciate if you could provide me some references, thank you.

Not directly. The config files are locked down by IIS so direct access is impossible.
You will have to go via Ajax to the server and request the setting.
Use this to make the call to the server asynchronously
http://api.jquery.com/jQuery.ajax/
You will need either a Web Method/ Service / Controller Action (if MVC) to handle the incoming request.
Alternatively send the value down in the initial page request via a hidden field or JavaScript variable set.

IIS by default will not serve Web.Config (or a selection of other file types as well) for reasonably obvious security reasons so you'd need to return your application setting to jQuery via an Ajax call or similar.

I would add a asp Hidden field and then set its value on page load from web.config.
You can access that information from JQuery.

The answer is here :
Can i read data from web.config using JQuery?
"Jquery is javascript that runs in your browser, your web.config resides on your server..."
The simple answer is : Not directly , you will have to call a webSerivce method .
It is common that you get the web.config parameters in your code behind.

Related

java session in servlet the same as sessionstorage in javascript

I have a servlet which I call the following:
request.getSession().setAttribute("name", nameObj);
Can I access it from the following page using
console.log('IH HERE' + sessionStorage.getItem('name') );
It doesn't seem to work. Either js or jquery solution would be nice.
Thanks,
Scott
This won't work, for two reasons:
sessionStorage is client-side only; it's not sent to the server via HTTP requests and the server can't write it without talking to the client.
request.getSession() is server-side only, with a session ID stored in a cookie but nothing else stored in a client-accessible format.
You'll have to use cookies if you want to achieve this effect (read / write by both) or loop over the session and provide it all in the page somewhere (read only by client).

How to include client side JavaScript in ssjs in xpages

I would like to include some results from client side JavaScript (csjs) into my server side JavaScript (ssjs) in XPages.
e.g. on csjs i collect the screenwidth of a device via window.screen.availWidth
I would like to use the result further in my ssjs. How can I do this?
You need to send the screen width and height from you client script to you server script.
using QueryString (location.href=...../?open&width=xxxx&height=yyyyy),
setting a field on your xpage and to a partialrefresh or using extlib remote service.
There are probably several more ways of doing it ;-)
Another approach could be, you could put your result of CSJS calculation in a hidden input field and then get those values in your SSJS code.
Also you can look into this discussion in StackOverflow for some more ideas. It basically uses partial refresh and passes the values from CSJS as parameters.
XSP.partialRefreshPost('#{id:_element_to_refresh_}', {params: p});
You could then access those parameters in your SSJS code.
And I would be repeating Fredrik, "there are probably several more ways of doing it" ;-)

Dynamically writing js file

Is it possible to create dynamic js file creation?
e.g. I am referring MyJs.js using tag as usual, but source will be accompanied with a context value.
now what I expect, at server side, MyJs.js will be written according to the context value; just like a jsp page.
Is it possible?
For sure. I guess you're using Java (given you mentioned jsp). Just map the request (MyJs.js) to your jsp (or whatever framework dispatcher you use) in web.xml, and then make sure to set the content-type of the response to text/javascript.
If you give more details then you might get a more detailed respones :)
YES
just make sure your response header: Content-type set as: 'text/javascript';

With JS, jQuery, how do I save an AJAX response to a (text) file?

It seems like this question is asked periodically and the common response is "You shouldn't do that with AJAX anyway. Just set the window location to the file."
But I'm trying to request a file that doesn't actually exist out on the server anywhere. It's dynamically generated (by a Django view) given the GET/POST context parameters. The file I want to retrieve via AJAX, and then save to the client machine, is a text file (csv).
I can currently get the text to the client machine (and can verify this by seeing it in logging or an alert) but cannot then figure out how to save this text to a file inside of the AJAX success callback fn.
Essentially, is this possible, is it something JS can do? That is, to open file save dialogs for "files" that are actually AJAX response text?
From the browser's point of view, it doesn't matter if the file exists or not, it's just a resource on a server that it's requesting. I think you're going to need to do some version of "Just set the window location to the file". If you set the content type in the header to something that the browser doesn't recognize, I believe it will ask the user if they want to save it.
As others mentioned, you can't do it only with JavaScript.
IMO the best option would be the Flash 10+ FileReference API.
There are some good JavaScript wrapper libraries like Downloadify that provide a JavaScript API to access those methods.
Give a look to this demo.
This isn't something JavaScript (and therefore jQuery or anything other JS framework) is allowed to do, for security reasons. You may be able to do what you want to flash or another route, but not JavaScript. Bear in mind Flash has it's own slew of security restrictions for this as well.
(Yes, IE can do this via an ActiveX object, but I'm not counting that as a "solution" here)
Basically, no. Javascript cant save anything to the local machine due to security restrictions. Your best bet may be to have a signed applet that the user can trust to write the file, or put it in a textarea that they can then easily copy and paste into a new file.
Could you not use the PHP rename() function for this, instead of just Javascript? Call to a PHP file and pass the name of the file you want to copy along with where as parameters?
I have the same problem. You can try this
<button id="Save">Save</button>
<img src="MakeThumbnail.ashx?Image=1.jpg" id="imgCrop">
$("#Save").click(function (e) {
url = $("#imgCrop").attr("src")+"&Action=Save"
e.preventDefault(); //stop the browser from following
window.location.href = url;
});

How to delete a file with javascript?

Did not have luck with these examples:
Javascript File remove
Javascript FSO DeleteFile Method
Deleting a File
There are no special permissions on the file.
Is there a way to do this in JQuery?
The requirement is - a certain file must be deleted from the web directory when another page is loaded. There is no security issue as this is on a closed network.
Any help is appreciated.
Thanks.
With pure JavaScript, it can't be done. Using an AJAX call to a server side script that deletes the file would work though.
Javascript cannot delete files, it is prevented as it would lead to HUGE security vulnerabilities. THose links are for ActiveX controls that are handled through JS. Use a server side language.
You can't delete files over HTTP (well in theory you can, but it's not implemented.)
The easiest way is to set up a tiny server side script (e.g. in ASP or PHP) and to call that from JavaScript. The server side script needs the proper permissions to do the deletion, but otherwise there is no problem.
In PHP the start would look like this: (Not expanding solution to a fully secure one because you're not saying what platform you are on)
<?
// STILL INSECURE!!!!
// Do not use in any public place without authentication.
// Allows deletion of any file within /my/files
// Usage: filename.php?file=filename
$basedir = "/my/files";
$file_to_delete = $_REQUEST["file"];
$path = realpath($basedir."/".$file_to_delete);
if (substr($path, 0, strlen($basedir)) != $basedir)
die ("Access denied");
unlink($path);
?>
you would call the script like this:
http://yourserver/directory/delete_file.php?file=directory/filename
You cannot delete a file on a remote server using only JavaScript running in a visitor's browser. This must be done with a server-side script.
If you are doing this in a RESTFUL way, you would send an HTTP DELETE request.
jQuery's ajax method states that you can use the method parameter to specify 'DELETE' but notes that some browsers may not support it.
Obviously you will need a webserver which will accept a DELETE request, and apply some sort of authentication/authorization so that joe random visitor can't delete your files. I believe Apache's mod_dav will get you started here.
Javascript is a client side language. So you are not able to delete file on server directly. All examples that you provide may be used only for deleting files on your local machine but not into server.
But you may call some server page function that will delete file.
You can't delete files with JavaScript as it is run locally. So, it doesn't even touch external files.
You need to use a server side language that has access to editing the files such as PHP, RoR, or ASP.
You can however use jQuery to call the server side code via AJAX such as $.get or $.post and then the server side code deletes it and it would seem as though JS is deleting the files.

Categories