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.
Related
Help me!
I have this kind of code but it never goes to IF function?
What is wrong?
jquery is working but does alert kill IF funtion?
IF funtion is working without jquery.
$.get("www.example.com", function(data) {
var param = 'name'
param = param.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + param + "=([^&#]*)"),
results = regex.exec(data);
var paramValue = results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
alert(paramValue);
});
if (name == 'Bob') {
window.location = ("www.example.com");
} else {
window.location = ("www.example.com");
}
UPDATE
Hi here is the right code but I changed URL addresses.
When I put }); outside the if/else funtion it still doesn't work.
It will so "ketju" information right but it doesn't go any www.example after that.
So If/else is not working. I need this "ketju" information when user sign in to the website. I don't need to show this information just need to guide user to the right address. So this alert is not important.
Every user have interface information which contain lot of information about user.
Here is the code:
<script type="text/javascript">
$.get( "https://xxx.xx/learning/id2/bin/flash_api2?random=",
function( data ) {
var param = 'ketju'
param = param.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + param + "=([^&#]*)"),
results = regex.exec(data);
var paramValue = results == null ? "" :
decodeURIComponent(results[1].replace(/\+/g, " "));
alert( paramValue );
if (ketju == 'Sweden') {
window.location=("www.example.com");
}else{
window.location=("www.example.com");
}
});
</script>
Alert doesn't "kill" if, don't worry about that. However note that your if/else is outside the function. So if you want it executed together with alert and the regex, put the }); line after the if/else, not before it.
You are not defining a variable name and not assigning it the value Bob anywhere in your code (at least not it the shown code). You may want to change your if statement to
if (paramValue == 'Bob')
You will then get the parameter called 'name' from the regex, and the paramValue will give you the value of that parameter. So 'Alice', or 'Bob', or 'Hieronymous' or whatever.
(provided that there are no further problems with assigning the paramValue or with the regex)
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');
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?
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','');