0

I want to extract following values from the url mentioned below?

  • layout2
  • aqua

code:

var url = "http://localhost:8080/dflyers/edit.html?layout2&aqua"
var dataLayout = // I need url string (layout2) here
var dataTheme = // I need url string (aqua) here

Can some one help to get mentioned values using jQuery/Javascript? Thanks

Ahsan Khurshid
  • 9,383
  • 1
  • 33
  • 51

3 Answers3

2

Well, if you just want to get those values from the mentioned url, and not any complex url parser, the most simple solution is:

var url = "http://localhost:8080/dflyers/edit.html?layout2&aqua";
var query = url.split('?')[1].split('&');
var dataLayout = query[0];
var dataTheme = query[1];

alert('layout '+dataLayout);
alert('theme '+dataTheme);​

Example running in jsfiddle

davids
  • 6,259
  • 3
  • 29
  • 50
1
var url = "http://localhost:8080/dflyers/edit.html?layout2&aqua",
    qs = url.split('?')[1],
    dataLayout = qs.split('&')[0],
    dataTheme = qs.split('&')[1];

If you're getting the url from the location.href, you could use location.search instead to get just the querystring and skip a split().

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

If you're using jQuery then the BBQ plugin (by Ben Alman) has the functionality you are looking for:

http://benalman.com/projects/jquery-bbq-plugin/

Paul Osborne
  • 1,550
  • 1
  • 10
  • 11