Using a query string on my master page markup - javascript

I need to supply a variable that I have in a query string to tell which push pins to draw on my map. Currently I have to hard code the beer name as I have done with "Stella" in the if statement. Is there a way that I can refer to my query string (beerName=Stella) to change this instead. This is all on the master page btw. Any help would be much appreciated.
$(document).ready(function () {
$.ajax({
url: "Handler2.ashx",
type: "post",
data: { "Action": "SearchByLocation" },
success: function (data) {
for (var i = 0; data.Locations.length - 1; i++) {
var pushPin2 = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(data.Locations[i].Latitude, data.Locations[i].Longitude), null);
$("#newdiv li").append("<li><a href='" + "Main.aspx?beerName=" + data.Locations[i].beerName + "'>" + data.Locations[i].beerName + "</a></li>")
collectionInMemory = data;
if (data.Locations[i].beerName == "Stella") {
map.entities.push(pushPin2);
}
}
for (var j = 0; j < collectionInMemory.Locations.length - 1; j++) {
if(collectionInMemory.Locations[j].BeerName = "Stella") alert("datada");
}
},
error: function () {
alert("failure");
$("#result").html('There is error while submit');
}
});

You can use an inline ASP.NET snippet like so, and it will render the query string variable
if (data.Locations[i].beerName == <%= Request.QueryString("beerName") %>) {
map.entities.push(pushPin2);
}

Related

Pulling a Usable Link out of a JSON Object

I need to figure out how to have a link that I pull from a JSON object an ACTUAL link that the user can click and follow to the site instead of just text. I feel like it's gotta be a quick fix, but I can't seem to figure it out! Thanks for the help!!
function sqoot(URL) {
$.ajax({
url: URL,
method: "GET"
}).done(function(response) {
var deals = response.deals
var untrackedURL = $("#untrackedURL");
var couponInfo = $("#info");
for (i = 0; i < deals.length; i++) {
var newUntrackedURL = $("<a href='deals[i].deal.untracked_url'>" + deals[i].deal.untracked_url + "</a>");
couponInfo.append(newUntrackedURL)
}
})
};
Assuming your fetched data is correctly used, here's why your link doesn't work : the href is actually deals[i].deal.untracked_url instead of its content.
try this instead :
function sqoot(URL) {
$.ajax({
url: URL,
method: "GET"
}).done(function (response) {
var deals = response.deals
var untrackedURL = $("#untrackedURL");
var couponInfo = $("#info");
for (i = 0; i < deals.length; i++) {
var newUntrackedURL = $('' + deals[i].deal.untracked_url + "");
couponInfo.append(newUntrackedURL)
}
})
};
Without the generated JSON, I can't help you further if this solution doesn't helps.
Look like maybe you had a typo:
'deals[i].deal.untracked_url' should be 'deals["+ i +"].deal.untracked_url'
function sqoot(URL) {
$.ajax({
url: URL,
method: "GET"
}).done(function (response) {
var deals = response.deals
var untrackedURL = $("#untrackedURL");
var couponInfo = $("#info");
for (i = 0; i < deals.length; i++) {
var newUntrackedURL = $("<a href='deals["+ i +"].deal.untracked_url'>" +
deals[i].deal.untracked_url + "</a>");
couponInfo.append(newUntrackedURL)
}
});
Scratch that - you want it to pull the value not write out "deals[i].deal.untracked_url." To do that you do the below.
function sqoot(URL) {
$.ajax({
url: URL,
method: "GET"
}).done(function (response) {
var deals = response.deals
var untrackedURL = $("#untrackedURL");
var couponInfo = $("#info");
for (i = 0; i < deals.length; i++) {
var newUntrackedURL = $("<a href='"+deals[i].deal.untracked_url+"'>" +
deals[i].deal.untracked_url + "</a>");
couponInfo.append(newUntrackedURL)
}
});

jQuery AJAX call freezes all other JS logic

I am trying to get a *.srt file and parse it with this script: Parse a SRT file with jQuery Javascript.
I've got a problem with the AJAX call blocking rest of the JS code. I tried to add syncs, set timeout, I wrapped it into a setTimeout function, tried with another *.srt file, but still it's not working. It doesn't throw error, it alerts end dialog, parsed lines are stored in variable but another scripts are frozen.
var subtitles = [];
$.ajax({
method: "GET",
url: '{% static "file.srt" %}',
async: true,
error: function(data) {
alert("error");
},
success: function(data) {
function strip(s) {
return s.replace(/^\s+|\s+$/g, "");
}
srt = data.replace(/\r\n|\r|\n/g, '\n');
srt = strip(srt);
var srt_ = srt.split('\n\n');
var cont = 0;
for (s in srt_) {
st = srt_[s].split('\n');
if (st.length >= 2) {
n = st[0];
i = strip(st[1].split(' --> ')[0]);
o = strip(st[1].split(' --> ')[1]);
t = st[2];
if (st.length > 2) {
for (j = 3; j < st.length; j++)
t += '\n' + st[j];
}
//define variable type as Object
subtitles[cont] = {};
subtitles[cont].number = n;
subtitles[cont].start = i;
subtitles[cont].end = o;
subtitles[cont].text = t;
document.body.innerHTML += " (" + subtitles[cont].start + " - " + subtitles[cont].end + " ) " + subtitles[cont].text + "<br>";
}
cont++;
}
alert("end");
},
timeout: 2000,
});
Please help me.

Can't pass an array and a string into controller with JavaScript

I hope someone can help me with this issue that I have.
I'm trying to pass an array into controller with JavaScript like this:
$('#send') .lick(function () {
var items = {};
var grid = $('#grid') .ata('kendoGrid');
var selectedElements = grid .elect();
for (var j = 0; j < selectedElements .ength; j++) {
var item = grid .ataItem(selectedElements[j]);
items['grid[' + j + '] .arecidoCodigo'] = item .arecidoCodigo;
}
$ .jax({
type: "POST",
data: items,
url: '#Url .ction("Index", "Busqueda")',
success: function (result) {
console .og(result);
}
})
})
This works fine.
But when I tried to pass another value, in this case a string data, the array is sending null into the controller but the string value is not null.
This is my code when I tried to send another value, in this case the array and the string data:
$('#send') .lick(function () {
var items = {};
var grid = $('#grid') .ata('kendoGrid');
var selectedElements = grid .elect();
var enviarDest = $('#destinatario') .al();
for (var j = 0; j < selectedElements .ength; j++) {
var item = grid .ataItem(selectedElements[j]);
items['grid[' + j + '] .arecidoCodigo'] = item .arecidoCodigo;
}
var postData = { marcas: items, destinatario: enviarDest };
$ .jax({
type: "POST",
data: postData,
url: '#Url .ction("Index", "Busqueda")',
success: function (result) {
console .og(result);
}
})
})
And this is the code of my controller:
public async Task<ActionResult> Index(List<MarcaParecido> marcas, string destinatario)
{
..
}
Here is when I receive the list marcas null and the string destinatario is not null, I don't know what am I doing wrong.
Try using JsonResult instead of async in your controller:
public JsonResult Index(List<MarcaParecido> marcas, string destinatario)
{ .. }

Why this button works in IE but not in Firefox?

I have strange issue and I don't know how to fix. This button works perfectly in IE , but not in Firefox.
This is the HTML code:
<div class="divbutonSave" onclick="Save()" style="float: right;">
<span>Save</span>
</div>
And this is the Javascript code:
function Save() {
var poz = '1';
var rowCount = dataTable.rows.length;
var ala0 = "";
for (var i = 1; i < rowCount; i++) {
var c = document.getElementById("chk " + i);
if (c.checked == 1)
ala0 += dataTable.rows[i].cells[1].innerText + "^";
var ala = ala0.substring(0, ala0.length - 1);
}
$.ajax({
url: '/ProjectAdministrator/ProjectAdministratorProject/Partners',
data: { pozActivity: poz, listapart: ala },
dataType: "Json",
type: "POST",
error: function () {
alert("Error");
},
success: function (data) {
window.close();
}
});
}
Any suggestions? Thanks in advance
I'd suggest if you want a button, you should use a button in your markup, rather than a div. That way your markup will represent what it means, and it may have the side effect of working properly too.

jquery problem in IE with dynamic dropdown selection

Hi jquery/javascript gurus,
I am trying to use jquery ajax function to populate the dropdown, it works fine with FF, but IE give the javascript error snow below in the scrnshot. howver IE does get the data and selects it.
Am i doing something wrong?
function getAjaxFunction(thisval, curval) {
$.ajax({
type: "POST",
url: "lookup.do?param="+thisval,
cache: false,
success: function(data) {
var values = data;
var vals = values.split(";");
$("#dropdown").find("option").remove().end();
for (var i = 0; i < vals.length; i++) {
var parts = vals[i].split(":");
$("#dropdown").append($('<option />').val(parts[0]).text(parts[1]));
}
$("#dropdown").val(curval);
}
});
}
You say val(curval) at the end of your function, but your function parameter is named currval with two Rs.
This worked!
function getAjaxFunction(thisval, curval) {
$.ajax({
type: "POST",
url: "lookup.do?param="+thisval,
cache: false,
success: function(data) {
var values = data;
var vals = values.split(";");
$("#dropdown").find("option").remove().end();
for (var i = 0; i < vals.length; i++) {
var parts = vals[i].split(":");
$("#dropdown").append($('<option />').val(parts[0]).text(parts[1]));
}
try {
$("#dropdown").val(curval);
} catch(ex) {
setTimeout("$('#dropdown').val('"+curval+"')",1);
}
}
});
}

Categories