Setting up a javascript url - javascript

I simply want to set up an ajaxurl in javascript to pass to my controller.
When trying to setup the url, it evaluates to 0 during the below attempts:
Note that attrs.attachmentType & attrs.attachmentId have values associated with them.
var hoser = attrs.attachmentType & attrs.attachmentId;
ajaxUrl = root + 'FileUpload/upload?' & 'Type=' & attrs.attachmentType & 'ID=' & attrs.attachmentId;
If I just include the following, a proper value is seen inside the hoser variable.
var hoser = attrs.attachmentType;
What am I doing wrong where the url will not come out correctly?

You are using & symbols, not adding them to the string. By using these symbols, javascript is interpreting the right hand side of your statement to be a comparison and outputting the answer it thinks you want - in this case, a boolean representation of false: 0
Change your & symbols to strings. Like so:
ajaxUrl = root + 'FileUpload/upload?' + '&' + 'Type=' + '&' + attrs.attachmentType + '&' + 'ID=' + '&' + attrs.attachmentId;

Related

Prevent Javascript from replacing beginning of string with NaN?

I am work on some OData query calls to Microsoft CRM and need my query in a very specific format.I am passing parameters to a function which then adds the URL to my query. What I am passing my retrieve function is as follows:
webAPI.REST.retrieveEntity(
"EntityDefinition",
id,
+ "/Attributes(LogicalName='" + logicalAttribute + "')"
+ "/Microsoft.Dynamics.CRM.PicklistAttributeMetadata"
+ "?$select=LogicalName&$expand=OptionSet"
, null)
In debugging my parameter with the query options is:
"NaNmylogicalattribute')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet"
As you can see, my "/Attributes(LogicalName=" was replaced with "NaN". How do I prevent this from occurring?
Here is the problem:
id,
+ "/Attributes(LogicalName='" + logicalAttribute + "')"
Since you aren't starting with a String, the leading + is coercing your String into a Number (it isn't a Number, hence NaN). Just remove that first leading + and it will work:
id,
"/Attributes(LogicalName='" + logicalAttribute + "')"

Define session variable in c# get it from javascript

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.

Encoding URL Query String Value from JavaScript to ASP.NET MVC

I have an app that uses ASP.NET MVC and JavaScript. I am generating a URL in my JavaScript like this:
// Imagine 'filter' is "AT&T".
var v = filter.replace(/'/g, "''");
v = v.replace(/&/g, "%26%");
var t = "(clause eq '" + v + "')";
// At this point, t is "(clause eq 'AT%26%T')"
window.location = '#Url.Content("~/find?")' + 'q=' + t;
In my ASP.NET MVC controller action, I have the following:
public ActionResult Find(int? id, string q)
{
// do stuff
}
If I set a breakpoint in the Find action, I notice that q is the following:
(clause eq 'AT&%T')
I'm not sure why. I need it to be (clause eq 'AT%26%T'). What am I doing wrong? I understand its some encoding issue.
The reason for this is that your encoded ampersand %26 is UrlDecoded serverside back to &. If you want to keep the url encoded version you need to escape the percent sign % too with its encoded equivalent %25:
v = v.replace(/&/g, "%2526%");

Javascript - concat string does not work as expected

What's the wrong with this Javascript line?
user: h.reem
domain: somedomain
var target = "//account/win/winlogin.aspx" +
"?username=" +
user.toString() +
"&domain=" +
domain.toString();
the resutl is always:
//account/win/winlogin.aspx?username=h.reem
Any idea!!
alert(user + "X") shows only h.reem
The ActiveX component is probably returning a null terminated string (I've seen this with Scripting.TypeLib & a couple of the AD objects for example) so concatenating it with another string fails. (You can verify this if 0 === user.charCodeAt(user.length - 1)).
You will need remove the last character before using the string;
user = user.substr(0, user.length - 1);
try:
var sUser = user.toString();
var sDomain = domain.toString();
var target = "//account/win/winlogin.aspx" + "?username=" + sUser + "&domain=" + sDomain;
The above might not fix your problem but it should expose it - Could be that your user.toString() method isn't returning a string and is short-circuiting things... If this doesn't answer your question I'd be glad to assist further, but it would be helpful if you posted the implementation or source of "user" somewhere ...

Why does eval() give undefined value in Javascript?

I am working in JavaScript coding. I have created a text area with name OQ_0 and value "0". When i use eval() method for that field in JavaScript it is giving the value undefined. The below are the part of JavaScript code
var tempOpenQtyStr = "document.InitiateReturnsForm.OQ" + "_" + 0;
var tempOpenxQtyStr = eval(tempOpenQtyStr).value;
alert('Manuals =' + document.InitiateReturnsForm.OQ_0.value);
alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));
alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);
Output:
Manuals = 0
eval(tempOpenxQtyStr ) = 0 --- Here it is suppose to show "[object]"
eval(tempOpenxQtyStr).value = undefined.
Kindly help me out what is change to do. Thanks in advance.
Why not just use document.InitiateReturnsForm["OQ_" + 0].value?
Try
alert('eval(tempOpenxQtyStr ) = ' + eval(tempOpenQtyStr));
alert('eval(tempOpenxQtyStr).value = ' + eval(tempOpenQtyStr).value);
In the second and third alert you are evaluating the second variable which stores the value of the first evaluated object. That's why the error occurs.
alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));
Since you put a string, not an object, inside tempOpenxQtyStr, it evaluates that string and returns 0.
alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);
Here you're using a method on a variable that contains a string. That doesn't work. It doesn't have that method, that's why it returns undefinied.
You might want to try doing eval(tempOpenxQtyStr.value) instead of eval(tempOpenxQtyStr).value since the last one does basically nothing, just evaluating an object and then fetching the objects value (it doesn't eval the value itself).

Categories