Passing parameters or variable using location() - javascript

I am trying to reload a page using location() in JavaScript and would also like to pass a variable value. below step didn't work for me. Let me know the correct procedure.
location='success.jsp'+<%=ID%>;

You'll want to do something like:
window.location='success.jsp?VARIABLE_NAME='+<%=ID%>;

Related

Pass Component Name as Argument and then attach method (not working?)

Maybe I'm not using the right terms/names for my searches but I have not been able to find anything on this topic. I would have thought this would be an easy thing to find. What I'm doing is passing a component name to a function and then trying to use the component's name by attaching a method to it. I have confirmed (via Dev Tools) that the name is correctly being passed but when I use the variable and attach the method the specific request does not work. If I 'hard-code' the exact component name to the method it works perfectly. This makes me think the (various) ways I've been trying to attach the method to the variable name is incorrect (see various attempts below). Can you offer some direction here? Thank you.
Passing to Function ...
const grid_name = "grid_GroupA";
console.log(grid_name); // Shows grid_GroupA
msg_max(newItem, grid_name);
Function (only listing relevant parts)
function msg_max(newItem, grid_target) {
console.log(grid_target); // Shows grid_GroupA
// grid_GroupA.data.add(newItem); // This works ...
// grid_target.data.add(newItem); // This does not work
// (grid_target).data.add(newItem); // This does not work
// [grid_target].data.add(newItem); // This does not work
// grid_target + '.data.add(newItem)'; // This does not work
Thank you ...
Edit ...
In my attempt to provide detail I hope I haven't confused the issue.
In essence, my question is if I can type this exact string
grid_GroupA.data.add(newItem);
and it works for my function, how can I place a variable with the exact string "grid_GroupA" in front of ".data.add(newItem);" and have it seen the same as the line of code that works? Maybe my lack of knowledge here is getting in the way but isn't the line of code that works just a string that is then used to 'find' the object? So, if that assumption is correct, how do I create that same string with the variable? If my assumption is wrong I am a willing learner so I will be all ears. Thank you.
I do not see how grid_target is an object. You are passing grid_name(which is a string) to the function, so grid_target will have no data property, because string doesn't have such a member.
P.S. snake_case is bad option for JavaScript, consider using cameCase instead

HTML Append Variable to Query String

I have http://localhost/?val=1
When I click on a link, is there a way this link can append a query variable to the current string, for example:
Link
so when I click it the url would be:
http://localhost/?val=1&var2=2
but when I click the link it removes the first query string and looks like
http://localhost/&var2=2
Is such a thing possible with normal HTML?
You can't do that using only html, but you can do it with js or php:
Using JS:
<a onclick="window.location+=((window.location.href.indexOf('?')+1)?'':'?')+'&var2=2'">Link</a>
Using Php:
Link
Notice 1: make sure you don't have the new variable in the current link, or it'll be a loop of the same variable
Notice 2: this is not a professional way, but it could work if you need something fast.
Basically you want to get your current URL via JavaScript with:
var existingUrl = window.location.href; // http://localhost/?val=1
Then append any Query Strings that are applicable using:
window.location.href = existingUrl + '&var2=2';
or some other similar code. Take a look at this post about Query Parameters.
Note: A link would already have to exist with an OnClick event that calls a function with the above code in it for it to work appropriately.
Now obviously this isn't very useful information on it's own, so you are going to want to do some work either in JavaScript or in Server code (through use of NodeJS, PHP, or some other server-side language) to pass those variable names and their values down so that the button can do what you are wanting it to do.
You will have to have some logic to make sure the query parameters are put in the URL correctly though. I.E. if there is only one query param it's going to look like '?var1=1' and if it's any subsequent parameter it's going to look like '&var#=#'.

JSON/AJAX and local variables in JavaScript functions

Working with JSON for the first time, I'm trying to call strings stored in a JSON file like this:
var getText = function(pat,id){
$.getJSON('assets/brospeak.json',function(js){
$('#pim1').append(js.pat.id);
});
}
So my intent is that pat and id get passed to getText when it's called to find the string. The problem is, the local variables in the line $('#pim1').append(js.pat.id); aren't getting called properly, so basically every time I call getText it looks for myfile.pat.id instead of, say, myfile.pattern.a. It works fine when I do just the getJSON part and explicitly tell it where the strings are.
The heck am I doing wrong?
Use js[pat][id] instead if the property name is dynamic:
var getText = function(pat,id){
$.getJSON('assets/brospeak.json',function(js){
$('#pim1').append(js[pat][id]);
});
}

Change Value of Instance Variable in Ruby

I have an instance variable in my controller and would like to increment it's value with Javascript. Am I able to do this? If so, how and where do I put this Javascript file?
A simple counter is all that I'm looking for. I'm not sure if I should look into calling AJAX instead. I need this to be done on the client-side without any page refreshes.
Yes, you will have to use Ajax to update your variable in controller. But I'm not sure how flexible an instance variable for this.
But have your variable in the session instead. Simple work flow will be:
you have a session variable
session[:counter] = 0
you have the increment code in the controller
def increment_session
session[:counter] += 1
end
and you will call the 'increment_session' via, AJAX from your view
assuming you are using Rails < 3, the following are some useful links for implementing AJAX call:
http://railsforum.com/viewtopic.php?id=19606
http://www.ibm.com/developerworks/java/library/j-cb12056/
http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote

How to set a javascript variable or reuse one from another function

I have a problem going on here and without going into a lot of detail and confusing everyone, let me just ask the simple question.
Here are two functions. I need to use the "id" variable in the SubmitForm() function as well. Can someone please tell me how to do this? This is how new I am to js. :)
Thanks for the help.
AC.chooseFunc = function(id,label)
{
document.qSearch.action = ".index.php?dc=2&id="+ id;
//document.qSearch.action = "index.php?dc=2";
document.qSearch.submit();
}
*** This one fails.
function SubmitForm(id)
{
document.qSearch.action = "index.php?dc=2&id="+ id;
document.qSearch.submit()
}
What I need is the "id" var appended to the query string in the SubmitForm Function. Can someone tell me how to do this please? Thanks for the help!
Can this not be done??
First, as a disclaimer, I think we would all be able to give you better answers if you post an example page demonstrating how the document is put together.
Here's one way you can make it work. At or near the top of your script (or <script> tag) declare a variable:
var storedId;
Then, at the top of AC.chooseFunc, copy the value of id to storedId:
AC.chooseFunc = function(id,label)
{
storedId = id;
...
Finally, remove id from SubmitForm's parameters and use storedId instead:
function SubmitForm()
{
document.qSearch.action = "index.php?dc=2&id="+ storedId;
document.qSearch.submit();
}
i dont know if you would want to do this, but you could make the variable global in other words declare it outside of both functions.
Could you not just use a hidden input and have PHP add it to the query string?
Is id null? Check the value of id...
alert(id);
The second function looks OK to me. The problem could be your passing null into it.
it's pretty clear that the two functions aren't being called in the same way.
As mentioned in another comment, alert the value to see what you're really getting in the function.
The function itself looks fine, so the only conclusion is that they are not working from the same data OR they are called in different ways.
I'd recommend posting a little more of the code in the call tree

Categories

Resources