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!
Related
I need the whole parameter list as such , not one by one
var Url = "http://localhost/Home/Admin?param1=1¶m2=2$param3=3";
I want to get the whole parameter list from the url.
var params = "param1=1¶m2=2¶m3=3";
var Url = "http://localhost/Home/Admin?param1=1¶m2=2$param3=3";
var urlArray = url.split("?");
var params=urlArray[1];
You can see Using split() example of Mozilla Developer Network for more insight on using the split function.
Thanks for the support, I use this one for my need
var params = window.location.href.split('?')[1];
I have for example, https://www.example.com/test1/something?asd=1. Of this example URL I need to grab everything up until and included /test1/. So I would set var url = https://www.example.com/test1. The problem is that test1 is dynamic so I can not have any hard coded values.
How can I do this?
One way is to use a combination of split() and join():
var url = "https://www.example.com/test1/something?asd=1";
var result = url.split("/",4).join("/");
Here's a JSFiddle of it in action:
http://jsfiddle.net/msm3jsvw/
I'm trying to put a string and a number together so a variable is in an image link, and so the variable formed by the string and number's value would display in the link, but instead the raw name just displays, not the variable's value.
var link1 = 'restOfLink.gif';
var myFunction = function(number){
var imgLink = 'http://i60.tinypic.com/';
var randomNumber = Math.floor((Math.random()*5)+1);
$("div").append("<img src = '"+imgLink+'link'+randomNumber+"'/>");
}
randomNumber won't always be 1 but let's just assume it is.
The link shows up as http://i60.tinypic.com/link1.gif
Any help on that? I'm trying to make an app that displays a new random image when a div is clicked. (the code above isn't all of the code, but it's enough to cover my problem)
If you wanted to create a dynamic img element with a variable as the link or part of it you could do:
//These are just example variables, you can use your own the same way.
var imgPath = '/images/blog/';
var size = '200x450';
document.createElement('img').src = imgPath + 'yourImage' + size + '.png';
JsFiddle Demo.
For your example i would suggest creating an array of links that you can access using a random number, for example:
var links = ['restOfLink.gif', 'anotherexample.png', 'yetanother.jpg'];
To select a particular image from inside the links array you need to pass it the index it occurs at, with 0 being the first.
So for example:
links[0] --> "restOfLink.gif"
links[1] --> "anotherexample.png"
links[2] --> "yetanother.jpg"
See this JsFiddle demo using this method.
You should create the link string by concatenating the different parts in Javascript, and update the img src attribute afterwards. Something like:
var bar = 3;
var linkString = "http://www.foo.com/image_number_" + bar + ".jpg";
document.getElementById("yourImgId").src = linkString;
Best.
Dont know what you want. A JSFiddle would help as suggested but here is my try at a answer:
string = "lol";
number = 1;
source = string+number;
$(".element").attr('src', source+".jpg");
You need JQuery for this though.
I want to get a specific part of a url between the third and fourth slashes of a link on the page.
EDIT: Sorry I don't think I was clear the first time, I meant getting the specific part of the url OF A LINK found on the page.
var getSegment = function (url, index) {
return url.replace(/^https?:\/\//, '').split('/')[index];
}
Usage:
getSegment("http://domain.com/a/b/c/d/e", 4); // "d"
The replace makes sure that the first two slashes after the protocol (http or https) don't count.
Here's a working example of getting a particular path segment.
Code:
var url = "www.test.com/one/two/three?p1=v&p2=v#anc";
var file = url.split('?')[0];
var pathanddomain = file.split('/');
var path = pathanddomain.splice(1, pathanddomain.length-1);
var pathIndexToGet = 2;
document.write(path[pathIndexToGet]);
If you want to do this for the current page, use:
var url = window.location.href;
Also, if your url starts with http(s)://, you will need to remove this.
I'd suggest:
var link = 'http://www.example.com/directory1/directory2/directory3/directory4/index.html';
console.log(link.split('/')[5]);
JS Fiddle demo.
The reason we're using [5] not [4] is because of the two slashes at the beginning of the URL, and because JavaScript arrays are zero-based.
you should elaborate you question and should specify which is your domain, that means on what purpose you are asking that question ??
This may help you:
var urlValue = url.split("/");
Then store urlValue as array.
then pick up third and forth value of the urlvalue on array.
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.