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)
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.
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?
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','');
i took a ready script from here, How to read GET data from a URL using JavaScript? and can't make it work, what im doing wrong?
here is my script:
function getUrlParam(param)
{
param = param.replace(/([\[\](){}*?+^$.\\|])/g, "\\$1");
var regex = new RegExp("[?&]" + param + "=([^&#]*)");
var url = decodeURIComponent(window.location.href);
var match = regex.exec(url);
return match ? match[1] : "";
}
var param = getUrlParam("process_number");
alert(param);
and here is my link:
http://erp.micae.com/index.cfm?fuseaction=objects.popup_print_files&process_number=SER-498&action_type=141&action_id=289&action_row_id=32&print_type=192
thx for the help!
Sorry guys, forgot to mantion that my page is working in a frame, that why it can't get the data from url i want :)
Since you're in a frame, if you need to get the href from the main window, do this:
var href = window.top.location.href;
Then process it.
That code has to run from the page whose URL you're mining for parameter values. In other words, it operates only on the current URL of the page it's on. It works fine.
If you want a function that gives you parameter values given an arbitrary URL, you'd just need to add an additional parameter:
function getUrlParam(param, url) {
param = param.replace(/([\[\](){}*?+^$.\\|])/g, "\\$1");
var regex = new RegExp("[?&]" + param + "=([^&#]*)");
url = url || decodeURIComponent(window.location.href);
var match = regex.exec(url);
return match ? match[1] : "";
}
alert(getUrlParam("process_number", "http://erp.micae.com/index.cfm?fuseaction=objects.popup_print_files&process_number=SER-498&action_type=141&action_id=289&action_row_id=32&print_type=192"));