This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have write following code to split the URL
var href = document.URL;
var GlobalBoomGuid = href.split("=");
var optionid = GlobalBoomGuid[1];
If i have URL like this : www.mydomain.com?optionid=655 It's giving me "655" .
But in current scenario my URL is www.mydomain.com?optionid=655#&ui-state=dialog and it's returning me 655#&ui-state
Now i want only id . How should i do that ?
Please if you dont like the question don't mark as a Negative
Thanx in Advance :)
This gets your result
var href = document.URL;
var foo = href.split("=")[1];
var GlobalBoomGuid = foo.split("#")[0];
var optionid = GlobalBoomGuid;
fiddle
Related
This question already has answers here:
How do I get the fragment identifier (value after hash #) from a URL?
(8 answers)
Closed 3 years ago.
I am trying to create a web app that uses a zip code (passed from the previous page). The URL looks like /location#12345
I am trying to store everything after '#' in a variable to be used in the location finder app.
URL: /locations#12345
$(document).ready(function(){
var queryString = decodeURIComponent(window.location.search);
queryString = queryString.substring(1);
var queries = queryString.split("#");
var myVal;
for (var i = 0; i < queries.length; i++)
{
myVal = myVal + queries[i];
}
alert(myVal);
});
I thought alert(myVal) would produce "12345" but it is blank. There are no errors in the console.
For a single hash String.split() is enough:
Working demo
const str = '/locations#12345UR';
const hash = str.split('#')[1];
alert(hash);
This question already has answers here:
How to use split?
(4 answers)
Closed 7 years ago.
I have a string like this 132+456 or 132-456 or 132*456..etc ,it changes dynamically but I need to split this into 132 and 456 how to do it using pure java script?
It should be as easy as:
var parts = '132+456'.split('+');
parts[0]; //132
parts[1]; //456
Like so
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
// document.writeln only for example
document.writeln(a);
document.writeln(b);
Java
String[] values='132+456'.split('+');
String firstPart=value[0]; // has 132
for js
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
This question already has answers here:
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 8 years ago.
Trying to add randomness to which image is displayed. Issue with saying calling and or statement.
var image = url3 | url2 | url1;
buildImage(image);
function buildImage(imagesrc) {
var img = document.createElement('img');
img.src = imagesrc;
document.getElementById('content').appendChild(img);
var images = [url3, url2, url1];
var image = images[(Math.random() * images.length)|0];
The "or" operators are not probabilistic "or" that will return one value or another. They are very specific: || will return the first value unless it's falsy, in which case it returns the second; and | converts both arguments to integers and applies the same operation to each individual binary digit.
Instead of using separate variables, make a urls array:
var urls = [
"http://example.com/img1.jpg",
"http://example.com/img2.jpg",
"http://example.com/img3.jpg"
];
You can then select a random URL by doing:
var url = urls[Math.floor(Math.random() * urls.length)];
And call buildImage:
buildImage(url);
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
My URL looks like this one:
http://www.something.hu/#/ask?kerdesPost=fdasdas%20ad%20asd%20ad%20asdas
I would like to get only fdasdas%20ad%20asd%20ad%20asdas or only kerdesPost=fdasdas%20ad%20asd%20ad%20asdas.
How can I do it via JavaScript? What is the shortest way?
You can try the following:
if((window.location.href).indexOf('?') != -1) {
var queryString = (window.location.href).substr((window.location.href).indexOf('?') + 1);
// "queryString" will now contain kerdesPost=fdasdas%20ad%20asd%20ad%20asdas
var value = (queryString.split('='))[1];
// "value" will now contain fdasdas%20ad%20asd%20ad%20asdas
value = decodeURIComponent(value);
// "value" will now contain fdasdas ad asd ad asdas (unescaped value)
}
JSFiddle
This should get you what you need.
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Get escaped URL parameter
(19 answers)
Closed 9 years ago.
I have this url index.html#secondPage?name=the%20second%20page
I want to get the value of name ("the second page") using javascript and jquery
thanks
You can use the code from the answers to this question. The only difference is that you want to parse the location.hash instead of location.search so just change that in whichever answer you choose to go with.
You'll also need to use substr to delete the leading # like:
var hash = window.location.hash.substr(1);
Here is the code from my answer to the question I linked to, with the modification:
function get_query(){
var url = location.hash.substr(1);
var qs = url.substring(url.indexOf('?') + 1).split('&');
for(var i = 0, result = {}; i < qs.length; i++){
qs[i] = qs[i].split('=');
result[qs[i][0]] = decodeURIComponent(qs[i][1]);
}
return result;
}
You can use it like:
var secondPage = get_query()['name']; // 'the second page'
Try like below, It will help you
Fiddle : http://jsfiddle.net/RYh7U/144/
Javascript :
function GetURLValue (sKey) {
return unescape("index.html#secondPage?name=the%20second%20page".replace(new RegExp("^(?:.*[&\\?]" + escape(sKey).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}
alert(GetURLValue("name"));