How replace query string values using jQuery? - javascript

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','');

Related

Parameter from URL Javascript - Form: /index.html#/?id=1426591453147

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 extract the parameter value from the url?

response.sendRedirect("http://localhost:8080/cse/welcome.html?first=fname&last=lname&dname=dept&mname=mobno");
How to extract the first,last,dname,mname parameters from the Url and I want to use those extracted values in my redirected html document(welcmoe.html). How can I achieve this?
If you wanted client side using JS, you could do the following.
//Create an array from query strings
//such as ['first=fname', 'last=lname', ...]
var querystring = location.search.substring(1).split("&");
var props = {};
//Loop thru the array
querystring.forEach(function(item) {
//split each item
var item = item.split("=");
//set the first part as key and last/second part as value
props[item.shift()] = item.pop();
});
alert ("first: " + props["first"]);
alert ("last: " + props["last"]);
//and so on...
The four variables from the java servlet can be hardcoded and passed as a string like this
StringBuilder url =new StringBuilder();
url.append("http://localhost:8080/cse/welcome.html");
url.append("?first="+fname);
url.append("&last="+lname);
url.append("&dname="+dept);
url.append("&mname="+mobno);
String URL=url.toString();
String urlEncoded=response.encodeRedirectURL(URL);
response.sendRedirect(urlEncoded);
Then these variables can be obtained and called in the javascript in this way
<script>
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 fname = getParameterByName('first');
var lname=getParameterByName('last');
var dept=getParameterByName('dname');
var mobno=getParameterByName('mname');
</script>

Get URL parameters & values with jQuery

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.

How do I make a more specific url for location.href.match?

I am using a series of if statements to manipulate css on a forum using the following:
if(location.href.match(/(showforum=3)/i) != null) {
$(document).ready(function(){
$("#topimg").addClass("announce");
});}
The code works perfectly fine, but every other showforum beginning with a 3 displays this image unless I code it otherwise. So my question would be how do I make my location more exact so that it only makes changes to 3 and not 3x? Is it even possible using this coding?
Change your regex so that the "value's boundary" is checked as well:
var pattern = /showforum=3(?=&|$)/i;
if (pattern.test(location.href)) {
...
}
Note the accompanying change in the testing expression: if you only need to find out whether or not some string matches the pattern, you should use regexp.test(string) syntax, not string.match(regexp) !== null.
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( getParameterByName("showforum") == 3 ){
//........
}
Ref
How can I get query string values in JavaScript?

Passing URL using javascript

Suppose that I have a URL like the following:
http://localhost:8000/intranet/users/view?user_id=8823
Now, all I want to do is to get the value of the URL using JavaScript and parse it, taking the user_id value (which is 8823 in this case) and sending that value through an iframe.
How can I do this?
try this code
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
i found it at How can I get query string values in JavaScript?
Try using window.location.href or document.URL
Do this:
var matches = document.location.search.match( /user_id=(\d+)/ );
if ( matches != null )
{
alert( matches[ 1 ] );
}
matches[ 1 ] will contain the user ID.
document.location.search contains the query string (all of the parameters which follow the '?' including the '?').
var test = "http://localhost:8000/intranet/users/view?user_id=8823";
//var url = document.URL;
var url = test.split("=");
var urlID = url[url.length-1];
document.write(urlID);
window.frames["myIframe"].yourMethod(urlID);

Categories