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

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

Related

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?

How replace query string values using jQuery?

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

JQuery: Retrieve data from URL

I am 'sending' some data in a url:
foo.htm?mydata
From searching around I know that I must use something like:
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
But I am a bit perplexed by this. Could someone help me out in deciphering this at all I simply want the end result to be placing mydata in a var, eg:
var newvar = mydata
Thanks heaps in advance!
The getParameterByName function (I assume you got it from here), is written to retrieve a query string value. This means, you can access data from the URL when it looks like this:
yourdomain.com/index.html?key=value&anotherkey=anothervalue
Now you can do this:
var firstKey = getParameterByName("key");
var secondKey = getParameterByName("anotherkey");
As described in your question, you don't have key/value pairs. If this is the case, and you only need the part after the ?, simply use the following by using the split method:
var newvar = document.URL.split("?")[1];
I do suggest you use the key/value method though, in case you want to pass on more variables in the future.
Maybe this library is interesting if you are in need of a lot of uri parsing:
https://github.com/medialize/URI.js

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