Say I have variables that I acquire in one html page, such as a UserName or a url or something. And in another html page I have input boxes for these variables and I want to autocomplete them by sending the data from the first html page to the input boxes in the second one. Can anyone indicate to me how I can achieve this?
Use JavaScript to create the equivalent collection for use by other JS code:
Code:
<script type="text/javascript">
var querystring = [ ];
var qs = location.search;
if ( qs.length > 1 )
{
qs = qs.substring(1); // skip past the ?
var pairs = qs.split( /\&/g ); // get all the name=value pairst
for ( var p = 0; p < pairs.length )
{
var pair = pairs[p];
querystring[ pair[0] ] = unescape( pair[1].replace(/\+/g," ");
}
}
</script>
Then, anyplace in your page where in ASP code you might use
Code:
var foo = Request.QueryString("foo");
you instead simply do
Code:
var foo = querystring["foo"];
CAUTION: "foo" will be case sensitive, unlike in ASP. If you wish, you could replace
Code:
querystring[ pair[0] ] = unescape( pair[1].replace(/\+/g," ");
with
querystring[ pair[0].toLowerCase() ] = unescape( pair[1].replace(/\+/g," ");
and then always use lower case names ("foo" in place of "Foo" or "FOO") when finding values.
Untested, though I have used this same code before. If there's a goof, it's just a typo.
You can use JQuery for that. Assuming you are not using any server side code.
Pass the value as a param to the next page.
Like myurl?param=xyz
Then you can get the value in the next page like this,
See this answer and sourcecode
var xyz = jQuery.url.param("param_in_url");
For this you can use php session .store that variable in session and get them in any page.or if you are calling that page from the page where u have values say username call like
next page
You should use $_SESSION variable in php. OR you can use sessionStorage of javascript.
Related
I want to set more than 1 url variable in JavaScript.
I am preparing a map, where only 1 url has been linied. I want to have more than 1.
I tried to use code like this:
var url = 'Peterborough.json';
var url = 'test.json';
but unfortunately only 2nd one is working. First one looks like switched off.
Does anyone knows how to place more than 1 url in the 1 line, to make them both working?
Thanks
A variable can have only one value at a certain time, what about using an array instead:
var url = ['Peterborough.json', 'text.json'];
console.log(url[0]); // => 'Peterborough.json'
console.log(url[1]); // => 'text.json'
You cannot declare two variables with the same name. You have two options here:
1- Simply renaming one of your variables, e.g.:
var url = 'Peterborough.json';
var url2 = 'test.json';
2- Use an array:
var urls = ["Peterborough.json", "test.json"];
//here urls[0] will be "Peterborough.json" (the first element of the array)
//and urls[1] will be "test.json" (the second element of the array)
You can do this:
var url0 = 'Peterborough.json';
var url1 = 'test.json';
console.log(url0);
console.log(url1);
I hoped it helped!
I am practicing making an e-commerce site. I am struggling passing my javascript variable between the shopping cart page to the checkout page. I tried following this example:
page one:
<a href="example2.html?myVar1=42&myVar2=66" >LINK</a>
page 2:
var qs = new Querystring();
var v1 = qs.get("myVar1");
var v2 = qs.get("myVar2");
I could not get it to work because my webpage threw an error about not recognizing QueryString, so I am currently trying like this by using window.localStorage as follows:
Page 01:
<button class="checkout" type="button" onClick="window.location.href='checkout.html'; window.localStorage.setItem("total",total)">Checkout</button>
Page 2:
var name = window.localStorage.getItem("total");
Currently, I am throwing a console.log(total) on the 2nd page but it returns "null" each time. Any idea why it's not catching the value from the first page? Please help.
If you have suggestions or a solution for either method, it would be much appreciated. I feel like I'm really close with the 2nd method, I may just be missing something on the page 1 portion, thanks in advance.
To fix your first method (passing variables between pages via a query string), you'd probably want something like:
// page one:
<a href="example2.html?myVar1=42&myVar2=66" >LINK</a>
// page 2:
var query = window.location.search;
if( typeof query !== 'undefined' ) {
var params = {},
parts
;
// take off the ? and split into groups of key=value strings
query = query.replace('?','').split('&');
// split key=value strings into a usable object
for(var i=0;i<query.length;i++){
parts = query[i].split('=');
params[ parts[0] ] = parts[1];
}
// now access your variables like so:
params.myVar1 // is now "42" (a string)
params.myVar2 // "66"
}
I am setting an attribute called "bikes" in the session, which is a List.
For instance, this code prints the firt element of the list :
function affiche() {
var myString = "${bikes.get(0).getName()}";
alert(myString);
}
I would like to use for instance a for loop and use something like
"${bikes.get(i).getName()}";
but I don't find a way to do it.
Any ideas ?
Thank you by advance
EDIT : with a java page, i am doing
session.setAttribute("bikes", bikes);
the only way I found to access the List in my jsp page is to do something like
var st = "$bikes";
but this gives something like [(filePathToObjectPackage#543622), (filePathToObjectPackage#54328)]. I think those numbers are related to memory. I can be wrong, but i thought the only way to access an object inside that list was to do
var myString = "${bikes.get(0).getName()}";
If so, i would like to be able to call it for a number contained in a variable.
I hope this is clearer now ...
Do you have try in this way:
function affiche() {
var myString = "";
for (var i = 0; i < bikes.length; i ++ ){
myString = "${bikes.get(i).getName()}";
alert(myString);
}
}
I want to get the my exact location (only PHP page) using javascript, I'm currently using window.location to get the exact url for example: localhost/empc/myfolder/functions.php what is the code if I want to get only functions.php as a result? Is it possible? Thank you.
You can use location object to do this, To remove the "/" before the file name you can use substring method.
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
you can try this pretty simple
var url = 'localhost/empc/myfolder/functions.php';
var url = url.split('/');
alert(url.pop());
It is possible. Use the javascript split function to separate the string into pieces and then access the last element to get the file name:
var str = "localhost/empc/myfolder/functions.php";
var arr = str.split("/");
alert(arr[arr.length-1]); //this will alert functions.php
This is driving me nuts, and I'm sure it's both possible and surely simple to do.
I have a page with a whole bunch of dynamically created forms on it. In one of my functions, I need to access one of those forms, so I pass the name of the form in a variable.
Then I need to access the name of that form using the document tree.
However, when I put in the variable, it assumes the name of the variable is the name of the form.
So this does not work:
function myAwesomeFunction(nameOfForm)
{
var selection = document.nameOfForm.nameOfInput.selectedIndex;
}
So I looked around the net and saw that I need to use bracket notation, but this doesn't work either:
function myAwesomeFunction(nameOfForm)
{
var selection = document[nameOfForm].nameOfInput.selectedIndex;
}
I also tried with some quotation action:
function myAwesomeFunction(nameOfForm)
{
var selection = document['nameOfForm'].nameOfInput.selectedIndex;
}
... but no joy.
So, where am I going wrong?
For bonus points... what if both the name of the form and the name of the particular input were both dynamic? Then what?
function myAwesomeFunction(nameOfForm, nameOfInput)
{
var selection = document[nameOfForm][nameOfInput].selectedIndex;
}
Look them up in the forms object - this won't work since it is an array and not an object.
use document.getElementsByName
function myAwesomeFunction(nameOfForm, nameOfInput)
{
var selection = document.getElementsByName(nameOfForm)[nameOfInput].selectedIndex;
}
or even better, set an id attribuite on the form and use document.getElementById to find the form
Try using document.getElementById(nameOfForm) (if you have the ID on the form as well)...
If you can include a jQuery reference to your page, you can easily do the following (again assuming you have the ID on the form):
function myAwesomeFunction(nameOfForm, nameOfInput)
{
var form = $("form#" + nameOfForm);
var input = $("#" + nameOfInput + ":input");
var selection = $(input).val();
}
function focusElement(formName, elemName) {
var elem = document.forms[formName].elements[elemName];
}
try this
formname is name of the form and elemname is input label name