I have a javascript function which contain file name, folder id, etc and store the values in cookies, and try to access the value in page load of "FileUploader.aspx". below is my javascript function
function cookieFileUploader(name, value) {
debugger
var date = new Date();
date.setTime(date.getTime() + COOKIEXPIRINGTIME);
var expires = "; expires=" + date.toGMTString();
//var encodedDocumentName = encodeURI(value);
//Encoder.EncodeType = "entity";
//var Encodedoc = Encoder.htmlEncode(value);
//document.cookie = name + "=" + Encodedoc + expires + "; path=/";
document.cookie = name + "=" + value + expires + "; path=/";
winstyle = "width=390, height=335,margin-top=0,scrollbars=no,toolbar=no, menubar=no,resizable=no,directories=no,location=no";
progressIndicatorWindow = window.open("FileUploader.aspx", "mywindow", winstyle);
}
now I want to pass the filename with japanese and chinese characters.while i am doing so the file changed in pageload with some square values. i tried encoding the file name in javascript using encodeURI and try to decode in pageload using server.htmldecode(). Is it right way? if not so please provide me a right solution.I want the filename in pageload event of fileUploader,.aspx
Use encodeURIComponent in javascript and then HttpUtility.UrlDecode on the server - that should do it.
Related
I use this code to change the cookie value in php, or set it if it does not exist yet:
setcookie('maintenance_site_contact_failed', '1', time()+3600, '/', 'entwicklung');
And I am using this code to set a cookie in javascript:
setCookie('maintenance_site_contact_failed', '0', 1);
function setCookie(name,value,days)
{
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
But as you can see in the following screenshot the cookie is created a second time instead of changing the existing one, but the duplicate has a dot in the domain name.
Why is there a dot and why is the cookie created twice?
What I try:
If a user sends my form and the validation fails on serverside, then
I am setting the cookie maintenance_site_contact_failed in the PHP script to 1 so that the client knows that an error happened.
The javascript then checks the value of the cookie, and if the cookie value is 1 then it shows an error and resets the cookie value to 0.
Looks like your cookie is only available to different subdomains because in the php equivalent you're assigning a value for the domain parameter.
Try to do the same inside the Javascript setCookie function:
document.cookie = name + "=" + (value || "") + expires + "; path=/; domain=entwicklung";
I am saving a URL in a cookie using Angular
$cookies.put("targetURL": "http://partners.api.xyz.net/apiservices/deeplink/v2?_cje=TmvlUQgTj%2fbxOQ%2bkXy5haJR9JWcHT9aaSjEP%2fEbwSq4uZqOYt%2fRV9FYsU1PT80PD&url=http%3a%2f%2fwww.apideeplink.com%26cabin_class%3deconomy%26facilitated%3dfalse%26ticket_price%3d72.82%26is_npt%3dfalse%26is_multipart%3dfalse%26client_id%3d2017-02-22T10%3a02%3a07")
However, I have to retrieve this cookie with plain javascript. When I try to get it, the string is transformed into
"http%3A%2F%2Fpartners.api.xyz.net%2Fapiservices%2Fdeeplink%2Fv2%3F_cje%3DTmvlUQgTj%252fbxOQ%252bkXy5haJR9JWcHT9aaSjEP%252fEbwSq4uZqOYt%252fRV9FYsU1PT80PD%26url%3Dhttp%253a%252f%252fwww.apideeplink.com%2526cabin_class%253deconomy%2526facilitated%253dfalse%2…e%2526is_multipart%253dfalse%2526client_id%253d2017-02-22T10%253a02%253a07" and not clickable anymore.
To retrieve the cookie I am using the function:
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
Why is the string changed like this and what can I do to solve this?
You need to decode the URI with decodeURIComponent()
old = "http%3A%2F%2F";
new = decodeURIComponent( old );
#new => http://
I'm saving string that represents the URL in a session variable from my code behind like this:
String mois = Request.QueryString["mois"].ToString();
String m = mois;
String moisnom = Request.QueryString["moisnom"].ToString();
String annee = Request.QueryString["annee"].ToString();
String dt = Request.QueryString["date"].ToString();
String user = Request.QueryString["user"].ToString();
String where = "jour.aspx?mois=" + mois + "&moisnom=" + moisnom + "&annee=" + annee + "&date=" + dt + "&user=" + user + "&cp=all" + "&usl=" + Request.QueryString["usl"].ToString();
Session["togo"] = where;
And then I try to get it like this in JavaScript like this:
var togo = '<%=Session["togo"]%>';
// i also tried this var togo ='#Session["togo"]';
var newPage = togo; // this should contain a string with the url to go to
But when I use it it uses it as a string here is what my URL looks like:
http://localhost:50311/<%=Session["togo"]%>
or
http://localhost:50311/#Session["togo"]
How else can I access the session variable or what am I doing wrong please?
EDIT:
like you suggested i already tried using the hidden field like this
yes i tried that but then i had this problem here is the definition of the hidden field
<input type="hidden" value="aa" id="myHiddenVar" runat="server"/>
then i tried giving it the value i need on click
String where = "jour.aspx?mois=" + mois + "&moisnom=" + moisnom + "&annee=" + annee + "&date=" + dt + "&user=" + user + "&cp=all" + "&usl=" + Request.QueryString["usl"].ToString();
myHiddenVar.Value= where;
and this is how i tried getting it from the js file
var togo = $('#myHiddenVar').val();
var newPage = togo;
but it takes the default value meaning "aa" as in value="aa" i gues cause the script is executed before the assignment of the variable any way how to reverse that order ?
After Session["togo"] = where;
save this Session["togo"] in hidden variable
hiddenVariable= Session["togo"];
Now in JS access that hiddenvariable:
suppose ID of hiddenvariable is "hdnxyz"
var togo = $('#hdnxyz').val();
var newPage = togo;
first of all session resides on server!!!!!!
If it is in different js file than you cant access it <%xyz%> i.e scriplet tags only work on aspx page...
so there is no way to access the session variable on client side..
instead assign your sessio9n value to a hidden variable and then access it using javascript
Write Session element in a asp:HiddenField and then read from it with your js code.
I have a task which is compare up to five products from the product list. For that
I have followed following steps:
step 1:
set onclick event when we click add to compare button of each product. In this event I have set cookie by javascript using this code.
// cookie is set by array because of we have to store 1 to 5 products
var comparearray = [productid];
document.cookie = "compareitem" + "=" + comparearray;
It is successfully set the cookie value which holds product id of those are selected to compare.
Step 2: In my PHP file I have tried to retrieve this cookie value.BY,
$cookie_val = $_COOKIE['compareitem '];
But it is not worked. I don't know this kind of concept is worth. If know, give me the instructions how to solve my problem. Thanks in advance.
Since it may be problem with path I suggest to set/get cookie with extended way:
document.cookie="foo=bar; path=/;"
or you can use this function:
function setCookie (name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
Since some people are writing that you can't use cookies set with JS in PHP, I'm going to answer that question now.
Please try to use a cookieSet and cookieGet function, you can use the one from this answer:
How do I create and read a value from cookie?
var createCookie = function(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
Note the last parameter which is written into the cookie.. the path-param is set to '/', so the root location! Use a php script in the root location, so that both use the root location and not some other location.
Next please try to stringify the array with JSON.
var cookiedata = JSON.stringify(comparearray);
Then you should be able to get the cookie with PHP and parse the JSON to get the array back.
Working on a local url: http://127.0.0.1:8000/qa/
Setting cookies using this JS code:
function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + newSize + "; " + expires; + "path=/";
}
If I call setCookie from /qa/, I can see that the following cookie has been set:
Name=font; Value=12px; Domain=127.0.0.1; Path=/qa
If I subsequently call setCookie from a different url (eg /qa/profile), I see that a second cookie has been created as follows:
Name=font; Value=12px; Domain=127.0.0.1; Path=/qa
Name=font; Value=18px; Domain=127.0.0.1; Path=/qa/profile
What I want is for any call to setCoookie to override the previous value of the initial cookie so that there is only ever one cookie with name="font" and that its value always reflects the last set value. I thought that specifying "path=/"; in my jquery function would do the trick. But it hasn't. What am I doing wrong?