This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I need to know what page i'm on based on the URL.
When i use $(location).attr('href')i get a very long url like this:
http://localhost:8181/research/gadgets/ifr?container=default&mid=0&nocache=0&country=ALL&lang=ALL&view=default&nerwVersion=~project.version~&url=http%3A%2F%2Flocalhost%3A8181%2Fresearch%2Fnerw-gadgets%2FCompanyDetails%2FCompanyDetails.xml&oaaParent=http%3A%2F%2Flocalhost%3A8181
The URL in the browser looks like this:
http://localhost:8181/research/?view=home
How do i get the view=home bit?
Try This:-
function getQuerystring(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(window.location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Usage getQuerystring("view");
Related
im trying to get a parameter from the current url using JavaScript or jQuery.
The URL looks like this:
http://my-site.com/index.html#/?id=1426591453147
or
http://my-site.com/#/?id=1426591453147
I tried a few codes with "location.search", but location.search return an empty string on my urls.
Does anyone know a good solution to this?
EDIT:
I ended up using this:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?]" + name + "=([^&#]*)"),
results = regex.exec(location.hash);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Something like this? (works with your specific case)
"http://my-site.com/index.html#/?id=1426591453147".replace(/^.*?\.com(.*?)$/i, '$1'); // "/index.html#/?id=1426591453147"
"http://my-site.com/#/?id=1426591453147".replace(/^.*?\.com(.*?)$/i, '$1'); // "/#/?id=1426591453147"
So,
location.href.replace(/^.*?\.com(.*?)$/i, '$1');
How to pass the values from one page to other page using the url in jquery.
I got few examples from google, below is my code but not able to pass the values.
Can any one help me on this ?
This is the function I always use in my work
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
from an SO answer here. Please note that you don't need to use jquery for this.
To use it:
Given a URL
http://www.example.com?testParam=value
get its value by
var myVal = getparameterByName("testParam");
I hope it helps.
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have a page here:
http://recipehappy.com/share.php?user=Mains&PID=2&passkey=bmAjWDMwmWcOBLoU4B0q2NARCG0PBTc1
I need to get the PID value and passkey value into JavaScript variable on the page. This may be simple, but I'm blocked.
Thanks for any help.
This is what I use
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
ex: getParameterByName('PID')
Source: https://stackoverflow.com/a/901144/643761
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I need to grab only the value 60408571 from the following url:
http://www.domain.com/ProductDetail.aspx?ProductId=60408571&this-is-a-page-title-that-goes-here
So far, I've successfully been able to grab everything after ?ProductId=, but this returns:
60408571&this-is-a-page-title-that-goes-here
The JavaScript I'm currently using is:
if(window.location.href.indexOf("ProductId") > -1) {
s.prop14 = window.location.search.replace("?ProductId=", "");
}
I only want to grab the numerical value if the page the user is on is a page with ProductId in the url.
Thank you for your help.
Thanks Tom. I modified my code, based on the reference Tom provided, and I now have:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
if(window.location.href.indexOf("ProductId") > -1) {
//s.prop14 = window.location.search.replace("?ProductId=", "");
s.prop14 = getParameterByName('ProductId');
}
Works great! Thanks again.
If the URL won't change you can also do this way:
var productId = findProductId(window.location.query);
function findProductId(url){
return url.split('&')[0].split('=')[1];
}
I have a problem , my original URL looks like this:
test.com/?manufacturer=0&body-style=0&min-price=270%2C000&max-price=780%2C000
As you can see, the min-price and max-price values in the query string is not correct due to the comma that is passed to the URL. It should be in their respective integer value like min-price=270000 and max-price=780000.
I need to convert the query string values of min-max and max-price using jQuery. I currently do not how to do this actually. But I have codes to get them from the URL and then convert them to the correct value. I just don't know how to implement them back to the URL (as new URL) using jQuery. These are my existing codes:
//Function to get value of parameter in query string
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Function to remove commas and convert to number
function convert_to_pure_number(x) {
//Remove commas
var x_withoutcommas=x.replace(/,/g,'');
//Convert to plain number
var y =parseInt( x_withoutcommas ,10);
return y;
}
var min_price_original=getParameterByName('min-price');
var max_price_original=getParameterByName('max-price');
var min_price_converted=convert_to_pure_number(min_price_original);
var max_price_converted=convert_to_pure_number(max_price_original);
Any suggestions how will I continue the above code with the additional code to put them back to the URL posted? Thanks for any help.
UPDATE
This is the process:
Form will be posted to the server--> URL will contain commas --> My new code will remove the comma --> In the query string value, correct value will be used.
Cheers.
use replace function like this :
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var min_price_original=getParameterByName('min-price').replace('%2C','');
var max_price_original=getParameterByName('max-price').replace('%2C','');