i want to get hash parameters value in my java script
for example my url should be like that
www.example.com/#!mydata=data&myopt=option
i want to get mydata in variable which will be "data" as value,
and myopt "option"
iam trying to implement google ajax cowling like here
Google Code
and i have tried to implement jquery address
but with big fail so help me either by solving 1st part or give me simple walk through tutorial to implement jquery address to my ajax requests ,thank you
This piece of code will convert any well formed (i.e. properly url-encoded) request string into an object literal with values parsed.
var s = "#!mydata=data&myopt=option";
var o = {};
$.each(s.substr(2).split('&'), function(i, elem) {
var parts = elem.split('=');
o[parts[0]] = parts[1];
});
Then you can access values like o.myopt
UPDATE
Of course, to get the value from browser's address, you should use
var s = window.location.hash;
Related
I'm using ArcGIS javascript to pass some value from map service to front-end HTML.
I use ${parameter_name} syntax and pass it into HTML by
var html = [];
html.push('<div>Parameter value is ${parameter_name}</div>');
InfoTemplate.setContent(html);
So that my HTML page will show an InfoTemplate holding
Parameter value is XXX
But now I want to get the value XXX and edit it before push it into html. How should I get the value in my javascript?
You can use a function in the parametrized template like this below:
myEditFunction = function(parameterValue) {
//here you can edit the value before returning it
return parameterValue;
}
var html = [];
html.push('<div>Parameter value is ${parameter_name:myEditFunction}</div>');
InfoTemplate.setContent(html);
See the documentation vor API v3.26: https://developers.arcgis.com/javascript/3/jshelp/intro_formatinfowindow.html especially the Using custom functions section.
Not wanting to bloat up an .htaccess with 300 entries, what would be the javascript I could use to redirect to URLs based on a query string in the request to this single file. For example,
https://www.mywebsite.com/redirect.jhtml?Type=Cool&LinkID=57
The only part I care about is the 57 and then redirect it to wherever:
https://www.anothercoolwebsite/secretworld/
In the following case, take the 34 and redirect:
https://www.mywebsite.com/redirect.jhtml?Type=Cool&LinkID=34
https://www.anoldwebsite.com/cool/file.html
Thank you!
This should do you fine. Keep in mind a server-side solution like a PHP script will work for more clients. Since you mentioned .htaccess, I think I should let you know about the fallback resource command
Anyways, here is the JS only solution
function parseString(){//Parse query string
var queryString=location.search.substring(1);//Remove ? mark
var pair = queryString.split('&'); //Key value pairs
var returnVal={};
pair.forEach(function(item,i){
var currPair = item.split('=');//Give name and value
returnVal[currPair[0]]=currPair[1];
});
return returnVal;
}
var links=["index", "about"];//Sample array of links, make sure this matches up with your LinkID
location.href=links[parseString().LinkID]+".html"; //Redirect based on LinkID
So, I'm working on a MULTIPLE CHOICE QUESTION entry page and i want to handle it completely with ajax. I want to be flexible with the number of options the question has.
Here's the jquery part:
$("#QuestionModPageSubmitButton").click(function(){
var QuesDesc=$("#QuesDesc").val();
var Options=[];
var QuestionId=$("#QuestionId").attr("data-id");
var CorrectOption=$('input[type="radio"]:checked').val();
var TotalOptions=$("#TotalOptions").attr("data-total");
var SubjectId=$("#SubjectId").attr("data-id");
for(var i=0;i<TotalOptions;i++)
{
Options.push($("#Option"+i).val());
}
$.ajax({
type:"POST",
url:"ajax/ModifyQuestion.jsp",
data:{
Subject:SubjectId,
QID:QuestionId,
Question:QuesDesc,
OptionValues:Options,
Correct:CorrectOption,
TotalOptions:TotalOptions},
});
});
I want to sent the Options Array to the jsp page "ModifyQueston.jsp".
Here's the jsp code i use for reading the sent data:
int SubjectId=Integer.parseInt(request.getParameter("Subject"));
int QuestionId=Integer.parseInt(request.getParameter("QID"));
String Question=request.getParameter("Question");
String[] Options=request.getParameterValues("OptionValues");
int CorrectOption=Integer.parseInt(request.getParameter("Correct"));
int TotalOptions=Integer.parseInt(request.getParameter("TotalOptions"));
But with these codes I'm not able to read the array in the jsp page. I get NullPointerException when i try to read the length of the Options array or when i try to read values by providing index.
I guess the script part of sending the data to jsp is fine. So the question is how to get it into jsp page.
I tried converting the array into a single string by separating each value with a '-' and then reading it using getParameter() function and then using split() function to separate it back to Array.
Script:
var OptionsString="";
for(var i=0;i<TotalOptions;i++)
{
Options.push($("#Option"+i).val());
OptionsString+=(Options[i]+((i<TotalOptions-1)?" - ":" "));
}
JSP:
String[] Options=(request.getParameter("OptionValues")).split("-");
It works fine. But I don't want to do it this way because if any of the options already contains '-' the Code will crash.
So, how to get this done?
Okay, so after a couple of weeks of research I found out a way to send the array from js to jsp. The previous code needed just a little modification.
Here's the js part which needed modification.I just had to add brackets as in arrays, in the data section.
$.ajax({
type:"POST",
url:"ajax/ModifyQuestion.jsp",
data:{
Subject:SubjectId,
QID:QuestionId,
Question:QuesDesc,
Correct:CorrectOption,
"Options[]":Options
},
});
Notice that I wrote Options as "Options[]". This makes jsp understand that the variable being sent is an array.
Now, the jsp page didn't really require much modification.
String[] Options=request.getParameterValues("Options[]");
And any further operations can be performed as normal strings.
So, yeah that worked for me!..
You can sending multiple value by ajax. From the controller end (for spring framework) you just save it in a string. the data will bind with comma separated values. to do that, you need an array from javascript end, i mean your jsp side.
for checkbox you can use:
var idVal = [];
var i = 0;
$('.case:checked').each(function() {
idVal[i] = $(this).val();
i++;
});
From controller side you can get the value:
String[] id = req.getParameter("id_").split(",");
As the same way you can do this for dropdown (options).This is worked for me when using spring framework.
Thanks.
I am returning SQL Query result as a JSONArray to a JSP page. Now i want to show the data. I have written a code but it is working fine only for 23 objects in the JSONArray if JSONArray contains more the 23 object eval or JSON.parse function doesn't work. Please let me know how to solve this problem.
Below is the JS code i have written to iterate over this JSONArray.
var data = '<%=(JSONArray) request.getAttribute("resultArray")%>';
data = eval("(" + data + ")");
$(document).ready(function() {
var table = $('<table/>').appendTo($('#column'));
var rows = $('<tr/>').appendTo(table);
$.each(data, function(rowid, row) {
var rows = $('<tr/>').appendTo(table);
$.each(row, function(column, data) {
($('<td/>').text(data)).appendTo(rows);
})});
});
Just don't let JSP print it as a JS string syntax within quotes (which obviously needs to be parsed in order to get a JS object). Get rid of those quotes. JSON is already in proper JS object syntax. That's also all what "JSON" stands for.
var data = <%=request.getAttribute("resultArray")%>;
$(document).ready(function() {
// ...
});
By the way, using scriptlets in JSP is a poor practice. If you're on JSP 2.0 already (which is out for almost a decade already), just use EL.
var data = ${resultArray};
$(document).ready(function() {
// ...
});
Note, also here, just don't quote it. It becomes otherwise a JS string instead of a JS object.
Unrelated to the concrete problem, is it absolutely necessary to introduce the extra JSON/jQuery step here? Why don't you just use for example JSTL to let JSP generate the desired HTML in the server side instead of JS/jQuery in the client side?
I have an Object and I'm trying to use jquery to quickly change the parameter values, but the parameters keep coming back null. code brings back the list of parameters but I can't seem to change anything. Even if I put it at it's base of parameters to change everything - it still comes back as null.
Other than that it works, but if u look closely you will see some api error messages in black at the top left. I added a pastebin so you can see what I'm doing.
http://jsfiddle.net/f4qMe/
and below is the javascript I'm running to try and change the objects parameters. The object is called (id) twitchTV.
function test(){
var data = "http://www.twitch.tv/widgets/live_embed_player.swf?channel=day9tv";
var src = "hostname=www.twitch.tv&auto_play=true&start_volume=25&channel=day9tv";
var code = $("#twitchTV").html();
var newcode = $("param", code).attr("value", src).html();
$("#twitchTV").html(newcode);
$("#twitchTV").attr("data", data);
}
Your problem is probably here:
var code = $("#twitchTV").html();
var newcode = $("param", code).attr("value", src).html();
html() returns a string so code is a string and you're using it as context in newcode which expects an DOM element or jquery object instead.