access javascript variable into the .ashx page - javascript

I have a JavaScript, which returns 2 variables. I just want to access those variables in the generic handler(ashx) page but I can't. Can anybody give some suggestion?
var myArray = [txt, value];
var url = "insertComments.ashx?dat=" + myArray.join();

Change your Javascript :
var url = "insertComments.ashx?datTxt=" + txt + "&" + "datValue=" + value;
and in handler access that values with :
string txt = context.Request.Params["datTxt"];
string val = context.Request.Params["datValue"];

Related

Passing a List<Map> through location.href

My problem is, I want to pass a parameter which is a List to my location.href
function addNewDriver()
{
var pEntityType = <%=c.WIDOC_ENTITY_DRIVER%>;
var pListEntities = <%=lListDrivers%>;
location.href= "<%= c.url %>do/user/groupItemForm.step1.jsp?idClient=" + <%=pIdClient%> + "&listEntities:" + pListEntities +
"&idGroup=" + <%=pIdGroup%> + "&entityType=" + pEntityType + "&<%= lBackButtonUrl %>";
}
This is an example in Chrome console.
var pListEntities = [{dniPerson=4444444S, surname2Person=XXX, passportPerson=null, namePerson=XXX, idGroupItem=1, idPk=1111, surname1Person=XXXX};
This is how I think "they" wanted it.
var pListEntities = [{dniPerson:4444444S, surname2Person:XXX, passportPerson:null, namePerson:XXX, idGroupItem:1, idPk:1111, surname1Person:XXXX};
"lListDrivers" is already filled with values of each driver, like dni, name, surname etc.
The problem comes because, to pass this list, values inside can't have an equal, they need a ":", but I don't know any way to change it.
Convert your List<Map> to JSON. you can use Jackson ObjectMapper to convert it and it will change your pListEntities to a JSON object.
Here is one example.

Get data from script tag

How can i get the email address from this script
<script>
var wcRQlKjfP = 'sales#websaucestudio##.com';
var wcRQlKjfP = wcRQlKjfP.split('#');
document.getElementById('wcRQlKjfP').innerHTML = wcRQlKjfP[0]+wcRQlKjfP[2]+wcRQlKjfP[1]; document.getElementById('wcRQlKjfP').href = 'mailto:'+wcRQlKjfP[0]+wcRQlKjfP[2]+wcRQlKjfP[1];
</script>
Help me!
Thanks a lot!
Although your variable has a strange name, all you need to do is combine the three parts (wcRQlKjfP[0] + wcRQlKjfP[2]).
wcRQlKjfP[1] can be completely ignored, as it evaluates to an empty string.
Here I'm logging the result to the console, but you could always assign to a new variable and then reference that variable.
var wcRQlKjfP = 'sales#websaucestudio##.com';
var wcRQlKjfP = wcRQlKjfP.split('#');
document.getElementById('wcRQlKjfP').innerHTML = wcRQlKjfP[0] + wcRQlKjfP[2] + wcRQlKjfP[1];
document.getElementById('wcRQlKjfP').href = 'mailto:' + wcRQlKjfP[0] + wcRQlKjfP[2] + wcRQlKjfP[1];
console.log(wcRQlKjfP[0] + wcRQlKjfP[2]);
<a id="wcRQlKjfP"></a>
Hope this helps! :)

replace url parameter and text using jquery?

When I click search button the url parameter is taking care of. I need to change that url parameter name and value using jquery.
per example: search button clicked
http://testsite/_layouts/OSSSearchResults.aspx?k=deana&cs=This%20Site
Replace with:
http://testsite/_layouts/OSSSearchResults.aspx?k=deana&s=All%20Sites
I can not modify search button functionality, because it is out of the box functionality.
I can do changing url parameter and value. How can we do that?
Changing the parameter of a URL should be fairly straightfoward:
var param = window.location.href;
var param_q = param.split('?');
var param_ampersand = param_q[1].split('&');
var param_eq_1 = param_ampersand[0].split('=');
var param_eq_2 = param_ampersand[1].split('=');
var new_param_name_1 = 'test';
var new_param_value_1 = 'example';
var new_param_name_2 = 'test2';
var new_param_value_2 = 'example2';
// avoid infinite loop
if ((param_eq_1[0] != new_param_name_1 || param_eq_1[1] != new_param_value_1) || (param_eq_2[0] != new_param_name_2 || param_eq_2[1] != new_param_value_2)) window.location = param_q[0] + '?' + new_param_name_1 + '=' + new_param_value_1 + '&' + new_param_name_2 + '=' + new_param_value_2;
This worked for me in Chrome/FF. This just redirects the parameter - not sure if this is what you're looking for.
EDIT:
Added a logic to handle two parameters.

Javascript eval

I am trying to get the following working. It seemed to work initially, but somehow it stopped working
var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + eval("setCommonAttr")).value;
what is wrong with above?
The above code is little different from what I am trying to accomplish. I gave the above example just not to make things complicated here. Below is what I am trying to accomplish:
First I am getting an existing element as follows. The element is a
<tr id="row_1_4_2009_abc" class="rowclick">
<td></td>
</tr>
I am using jquery to get the id on click of a row:
$(".rowclick").click(function() {
var row_id = $(this).attr("id");
var getAttributes = row_id.split("_");
var setCommonAttr = getAttributes[1] + "_" + getAttributes[2] + "_" + getAttributes[3] + "_" + getAttributes[4];
var new_row_id = document.getElementById("new_row_" + setCommonAttr).value;
});
You shouldn't need eval() to do this. The value you want is already a variable in JavaScript. Try:
var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + setCommonAttr).value;
Will everything be in that form? row_xxx_xxx_xxx_xxx
if so, why not var new_row_id = document.getElementById("new_" + row_id).value;
You don't need to call eval().
You can just concatenate the string with the variable:
var setCommonAttr = "1_row1_common"; var val = document.getElementById("abc_" + setCommonAttr).value;

need to extract certain values from attribute and place them in variables

In the following markup on a page I want to extract out the following and put them in seperate variables.
1- In the onclick attribute get the value after the "ProductID" and put it in its own variable. "ProductID"
So in this case it would be 318
2- In the onclick attribute get the value after the "Orig_price" and put it in a variable, "Orig_Price" So in this case it would be 22.95
3- In the onclick attribute get the value after the "width" and put it in a variable, "width" So in this case it would be 330
4- In the onclick attribute get the value after the "height" and put it in a variable, "height" So in this case it would be 300
<img src="/v/vspfiles/templates/100/images/buttons/btn_quantitydiscounts.gif" border="0" align="absmiddle">
UPDATED DEMO:
DEMO: http://jsbin.com/axuce3/3 SOURCE: http://jsbin.com/axuce3/3/edit
var pieces = $('a').attr('onclick').toString().split('?')[1].split('=');
var parts = [];
for (var i = 0; i < pieces.length; i++) {
var value = parseFloat(pieces[i]);
if (!isNaN(value)) parts.push(value);
}
alert( 'ProductID=' + parts[0] + 'Orig_price=' + parts[1] + 'width=' + parts[2] + 'height=' + parts[3]);
Please forget my previous answer, it was wrong (to test it, I assigned an id to the wrong element). You can get the value of the onclick attribute using getAttribute. For testing purposes, I changed your example to
<img src="/v/vspfiles/templates/100/images/buttons/btn_quantitydiscounts.gif" border="0" align="absmiddle">
Now document.getElementById("test").getAttribute("onclick") returns
window.open('/BulkDiscounts.asp?ProductID=318&ProductCode=' + escape('LB30X40ES') + '&Orig_Price=22.95', 'Discounts', 'scrollbars,status,resizable,width=330,height=300');
Now you can get the values using
var theString = document.getElementById("test").getAttribute("onclick");
var ProductID = theString.match(/ProductID=(\d*)/i)[1];
var Orig_Price = theString.match(/Orig_price=([\d\.]*)/i)[1];
var width = theString.match(/width=(\d*)/i)[1];
var height = theString.match(/height=(\d*)/i)[1];
(code is stolen from Tambourine's answer, so please give him the credits ;).
Try this code:
var string = $('a').attr('onclick') + ""; // to be sure var string is string
ProductID = string.match(/ProductID=(\d*)/i)[1];
Orig_Price = string.match(/Orig_price=([\d\.]*)/i)[1];
width = string.match(/width=(\d*)/i)[1];
height = string.match(/height=(\d*)/i)[1];

Categories

Resources