Send parameters to another page - javascript

I have a mystic problem with sending parametrs from one page to another.
In one of ExtJs methods i send parameter by POST to another page:
autoLoad : {
url : url_servlet+'form.jsp',
params: str,
scripts: true
}
But i dont know how to get this parametr in JavaScript. Okey i says, and sent parameter in url:
url : url_servlet+'form.jsp?ss=333'
And in another page:
function param(Name){
var Params = location.search.substring(1).split("&");
var variable = "";
for (var i = 0; i < Params.length; i++){
if(Params[i].split("=")[0] == Name){
if (Params[i].split("=").length > 1)
variable = Params[i].split("=")[1];
return variable;
}
}
return "";
}
var s =param('ss');
alert(s);
And see empty alert.
in firebug i try:
window.location.search
and get " ".
Whats wrong? I read several examples and everywere see code like this.

What's likely going on here is that ExtJS loads an entire page from a remote location into the current page.
When this happens, the code that gets run as a result of the load, will execute in the current page (which probably doesn't have the ss=xyz parameter at all).
However, your form.jsp should have access to the query string and can inject that into the page it returns to ExtJS.
Another option is to somehow pass that data from JavaScript once the page is loaded, but I don't know enough about ExtJS to tell you how that could be done.

Can you please try below function ?
function param(Name){
var Params = location.search.substring(1).split("&");
var variable = "";
for (var i = 0; i < Params.length; i++){
if(Params[i].split("=")[0] == Name){
variable = Params[i].split("=")[1];
return variable;
}
}
if(variable=="") return variable;
}
var s =param('ss');
alert(s);

You cannot get POST parameters from javascript. POST parameters are to the server and javascript is on the client side..
If its a GET then you could use parseUri library
var value = uri.queryKey['param'];

Related

Dynamic web page scraping with npm cheerio and and request

I'm trying to scrape data from a site which has a base url and then dynamic routes. This particular site simply uses numbers, so I have this code to get the data:
for (var i = 1; i <= total; i++) {
var temp = base_url + i;
var result = "";
request(temp, function(error, response, body) {
var $ = cheerio.load(body);
var address_string = 'http://maps.google.com/?q=' + $('title').text();
//firebase
database.ref('events/' + i).set({
"address": address_string
});
});
}
However, the above code doesn't work, and doesn't add anything to the database. Does anyone know what's wrong?
I'm not sure about the reason, but one thing that will behave strangely in the code you wrote is that the variable i is not bound to the callback scope of the request, and the for loop will finish before any callback is called.
If this is the problem, there should only be one db entry for i === total.
This can be solved by doing an Array.forEach instead.

Send Ajax request to many server?

I have problem with my code:
Now I want to send request Ajax to 2 page, is it ok ? if ok, can show me how to do. Thanks.
Example:
function change_select_employee(){
var p="";
p="&month="+document.getElementById('F02S').value;
document.getElementById('select_employee').innerHTML = "";
new Ajax.Request('a.php', { method:'get', onSuccess:onLoad_select ,parameters:p});
}
I want send this ajax to 2 file, a.php, b.php, how do I do it ?
As both comments suggest you can go both ways about this, either run the Ajax.Request twice or use an array to hold your target urls.
function change_select_employee(){
var p = "&month="+document.getElementById('F02S').value;
document.getElementById('select_employee').innerHTML = "";
new Ajax.Request('a.php', { method:'get', onSuccess:onLoad_select ,parameters:p});
new Ajax.Request('b.php', { method:'get', onSuccess:onLoad_select ,parameters:p});
}
or
function change_select_employee(){
destinations = ["a.php","b.php"];
var p = "&month="+document.getElementById('F02S').value;
destinations.forEach(function(dest) {
new Ajax.Request(dest, { method:'get', onSuccess:onLoad_select ,parameters:p});
});
document.getElementById('select_employee').innerHTML = "";
}
You can even define the target url array outside the main function and pass it as an argument to change_select_employee($destinations)
Create a function ( makeAjaxRequestTo(page) ) that sends an AJAX request to a given page, and then simply call it with a few pages, like this:
var pages = [ "/page1/somewhere.php", "/page2/somewhere.php" ];
var page;
for (var i in pages){
page = pages[i];
makeAjaxRequestTo(page); // Your own code goes here to send an AJAX request to page x
}

Why doesn't CasperJS' sendAJAX function send any content for PUT requests?

I want to update a JSON object though my app using the PUT method. I am attempting to partially simulate the operation of a form submission. This call is synchronous, as after the PUT, a 200 should be returned. However, when I try the code below, sendAJAX does not include the object's content, the PUT request has content-length: 0. A submission through the form has all of the correct settings - content-type, x-requested-with, etc. I'm not quite sure why the code below doesn't function as I'm expecting. I've also tried setting var 'data' as a string of json formatted parameters. That also results in a PUT request of content-length: 0.
What am I doing wrong here?
casper.then(function() {
this.evaluate(function() {
var element = document.querySelectorAll("h6");
for (var i = 0; i < element.length; i++) {
if (element[i].innerHTML == "Special Tag") {
var appid = element[i].parentNode.parentNode.getAttribute("app-id");
var wsurl = "https://appurl.net"+appid;
var data = new Object();
data.user_id = "xxxxx-xxxx-xxxx-xxxx-xxxx";
data.name = "Name";
data.description = "blahr blahr blahr";
data.amount = "-9000000";
data.start = 1409900400000;
data.finish = 1412492400000;
data.seq = 0;
data.locked = false;
data.paused = false;
data.contributed_amount = 0;
data.created = 1409920472782;
data.modified = 1426538857339;
data.color = "#E37368";
data.archived = false;
data.target_amount = null;
data.uuid = "xxxx-xxxxx-xxxxx-xxxxxx";
data.aprox_daily_contribution = 0;
return JSON.parse(__utils__.sendAJAX(wsurl, "PUT", data, false, { contentType: "application/json"}));
CasperJS' sendAJAX() function does not support PUT requests as seen in the code:
xhr.send(method === "POST" ? dataString : null);
Or more precisely, it only supports a payload for POST requests. You will have to build the XHR yourself or change the CasperJS code accordingly.
I don't think you need to simulate the form submission. If you're testing the web app, then it is better to actually submit the form and then check if it resulted in a correct page rather than parsing the response, because you will need to effectively implement the logic twice: in the page JavaScript and in your test script.

How to get cookie value in jquery which is set from C# class?

I have created and set the cookie through C# class and I want to use this cookie value in jquery file.
Like I have created and set cookie ...
HttpCookie myCookie = new HttpCookie("LoggedUserId");
myCookie.Value = newUser.UserId.ToString();
and this cookie value want to use in jquery file
var loggedUserId = $.cookie('LoggedUserId');
but it is returning undefined.
I also tried this..
var loggedUserId = '#HttpContext.Current.Request.Cookies["LoggedUserId"].Value';
But it is returning #HttpContext.Current.Request.Cookies["LoggedUserId"].Value as a string in loggedUserId.
Please suggest me to move forward. Thanks
If you are putting your code on page then your code will run perfectly
<script type="text/javascript">
var cookie = '#HttpContext.Current.Request.Cookies["mycookie"].Value';
alert(cookie);
</script>
But in case, you are adding your code in js file then this code might do your work.
function getCookieValue(name) {
cookieList = document.cookie.split('; ');
cookies = {};
for (i = cookieList.length - 1; i >= 0; i--) {
cookie = cookieList[i].split('=');
cookies[cookie[0]] = cookie[1];
}
return cookies[name];
}
Please see this link for more detail. Hope it can help you.
http://dotnet-concept.com/Article/2014/12/34/Create-Update-and-Get-cookie-value-through-javascript

accessing ASP.NET controls in Javascript

I have following Javascript line in my ASP.NET web app:
document.getElementById('<%=myTextBox[0].ClientID %>').value = "test";
how can I access myTextBox elements? for instance I want to change 5th element of this server side array, I want to pass a server side parameter to my function, how can I do it?
for instance:
server side:
ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + i + "');");
javascript function:
function OnFoodChange(myCmb,myIndex)
using document.getElementById('<%=myTextBox[myIndex].ClientID %>').value = "test"; gives me error, it says that myIndex is not defined, I think because myIndex is used like a server side parameter, how can I solve it?
it is my full JavaScript function:
function OnFoodChange(myCmb,myIndex) {
//alert('5');
try{
var q = document.getElementById('<%= HFFoodPrice.ClientID %>').value.toString();
var q2 = q.split(';');
var index = 0;
//alert(myCmb.selectedIndex.toString());
//var e = document.getElementById(myCmb);
var strUser = myCmb.options[myCmb.selectedIndex].value;
document.getElementById('<%=myTextBox[0].ClientID %>').value = strUser;
for (var j = 0; j < q2.length; j++) {
if (q2[j] != '') {
var q3 = q2[j].split(',');
{
}
}
}
}
catch(err)
{
alert(err.message);
}
}
Send this control id of textbox instead of sending index as asp.net control id might not be as you expect,
In Code behind
ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + myTextBox[i].ClientID + "');");
In HTML
function OnFoodChange(myCmb,myTextBoxId) {
currentTextBox = document.getElementById(myTextBoxId);
//Your code...
}
You are trying to Mix Server side with Client side. it does not work like this. you need to transfer your server array to javascript on server then you will be able access its index on clien side.

Categories

Resources