Getting a number from within a URL [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get query string values?
I am trying to get a page number from a URL during a test. The URL looks like this:
http://www.site.com/objects/search/8765XX/results?page=2&sort_att=posted_dt&sort_dir=desc
What I want to get is the page number right after 'page=' in the url.

In one line, by splitting url. jsfiddle
var url = "http://www.site.com/objects/search/8765XX/results?page=2&sort_att=posted_dt&sort_dir=desc";
var pageNumber = parseInt(url.split("page=")[1].split("&")[0], 10);

A simple pure JavaScript implementation I can think of would be something like the following:
var url = http://www.site.com/objects/search/8765XX/results?page=2&sort_att=posted_dt&sort_dir=desc
var matchedPos = url.search("page=\\d");
var matched = url.substr(matchedPos);
var num = matched.split("=")[1]; //might need to parse

I have this small function to fetch URL parameters (I found it on internet many years ago) :
function getUrlParameter(name, defaultValue) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return defaultValue;
else return results[1];
}
I use it like this :
pageNumber = parseInt(getUrlParameter('page'), 10);

Related

Split URL string to get each parameter value in javascript [duplicate]

This question already has answers here:
How to convert URL parameters to a JavaScript object? [duplicate]
(34 answers)
Closed 8 years ago.
var url = "journey?reference=123line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC"
How can I split the string above so that I get each parameter's value??
You could use the split function to extract the parameter pairs. First trim the stuff before and including the ?, then split the & and after that loop though that and split the =.
var url = "journey?reference=123line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC";
var queryparams = url.split('?')[1];
var params = queryparams.split('&');
var pair = null,
data = [];
params.forEach(function(d) {
pair = d.split('=');
data.push({key: pair[0], value: pair[1]});
});
See jsfiddle
Try this:
var myurl = "journey?reference=123&line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC";
var keyval = myurl.split('?')[1].split('&');
for(var x=0,y=keyval.length; x<y; x+=1)
console.log(keyval[x], keyval[x].split('=')[0], keyval[x].split('=')[1]);
to split line in JS u should use:
var location = location.href.split('&');

How to get the last two characters of url with jQuery?

I have a url with the following format:
base/list.html?12
and I want to create a variable which will be equal to the digits after the question mark in the url of the page. Something like:
var xxx = anything after the ? ;
Then I need to load dynamic data into that page using this function:
if(document.URL.indexOf(xxx) >= 0){
alert('Data loaded!');
}
How can I achieve this? and are the codes above correct?
Thanks
You can use split to get the characters after ? in the url
var xxx = 'base/list.html?12';
var res = xxx.split('?')[1];
or for current page url
var res = document.location.href.split('?')[1];
res = document.location.href.split('?')[1];
Duplicate of 6644654.
function parseUrl( url ) {
var a = document.createElement('a');
a.href = url;
return a;
}
var search = parseUrl('base/list.html?12').search;
var searchText = search.substr( 1 ); // removes the leading '?'
document.location.search.substr(1) would also work

JS - read value of submitted form [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get query string values in JavaScript
If one submits a GET form, the resulting address will look like www.example.com/stuff?param1=stuff&param2=morestuff. I know how to read/set the value of a form field on a page, but how do I read submissions from the previous page (in the URL) with JavaScript? I guess I could take the url and split() it, to get the parameters, but is there any quicker/simpler way to read param1 (just an example)?
Note: this is not a duplicate of this, since that question is about how to do it in PHP.
function getQuerystring(key)
{
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,\\\]);
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs != null)
return(qs[1]);
else
return("");
}
No, there's no simple way to do that.
Use something like this:
var qstring = {}, src = location.search.substring(1).split("&");
for (var i = 0; i < src.length; i++) {
var parts = src[i].split("=");
qstring[unescape(parts[0])] = unescape(parts.slice(1).join("="));
}
Now the object qstring is a key/value map of the query string. Keep in mind that values with the same key are overwritten, so you may want to store them in an indexed array instead of an associative array.
I think this topic is answer to your question.
function gup( 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 results[1];
}
You can try also this one
jQuery URL Parser Plugin. In case you want to use jQuery.
jsUri. In case jQuery is too heavy for you.

Get a parameter from a rewritten URL with javascript

I am trying to get a url parameter using javascript so i can pass the paramter to google maps
The problem is i'm using mod rewrite on the url
www.mysite.com/1/my-event
instead of
www.mysite.com/mypage.php?id=1&name=my-event
I've tried doing an alert but it comes up blank
Here is the javascript function that will work if i don't rewrite the url
function gup( 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 results[1];
}
The rewritten format, with the query-string, isn't available to your JavaScript.
You'll have to grab the value out of location.pathname (/1/my-event in your example), instead:
var params = window.location.pathname.split('/').slice(1); // ["1", "my-event"]
var id = params[0];
var name = params[1];
Just split the URL on / characters and take the last elements in the resulting array, mapping them to the names you expect.

url parameter to save is in JavaScript

I would like to insert a parameter value found the url into my javascript code.. So basically i have the following url www.rene-zamm.com/mp-dthanks.asp?gglid=123123123
Now i have the following javascript code in the same page and i want that number in the url to be visible also in the javascript code below:
var gwoTracker=_gat._getTracker("UA-1639156-3");
gwoTracker._trackPageview("/**NUMBER OF URL HERE**/goal");
Can someone help me please?
You need to parse the querystring. Javascript has no inbuilt way of doing this easily, but there are some nice functions you can use:
function gup( 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 results[1];
}
http://www.netlobo.com/url_query_string_javascript.html
Then in your example you could do:
var queryData = gup("gglid");
var gwoTracker=_gat._getTracker("UA-1639156-3"); gwoTracker._trackPageview("/" + queryData + "/goal");

Categories

Resources