-2

I have a url as string in javascript like follows :

http://localhost:8080/Blah.ff?param=1&param=2...

I want to filter the string and get

?param=1&param=2....

How can I do this in javascript?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212
  • possible duplicate of [Truncate a string straight javascript](http://stackoverflow.com/questions/1301512/truncate-a-string-straight-javascript) – highlycaffeinated Aug 09 '11 at 20:17

3 Answers3

4
var str= "http://localhost:8080/Blah.ff?param=1&param=2";
var str2 = str.substr(str.indexOf("?"), str.length);
alert(str2);

http://jsfiddle.net/praveen_prasad/a9XBe/1/

Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106
  • The second line of this can be simplified to: `var str2 = str.substr(str.indexOf("?"));`. Also, your solution assumes there is always a ? in the string. – jfriend00 Aug 09 '11 at 20:43
2
var urlstring = "http://localhost:8080/Blah.ff?param=1&param=2";
var querystring = '?' + urlstring.split('?')[1];

http://jsfiddle.net/ipr101/htUa3/

ipr101
  • 24,096
  • 8
  • 59
  • 61
-2

There are a lot of ways to do this. I would start with the window.location or location.href elements. Read up some more here:

http://www.w3schools.com/js/

Twisty
  • 30,304
  • 2
  • 26
  • 45
  • @kingjiv You know I read all the criticism on that page, and all of it was stupid nitpicking. All the real stuff was fixed by w3schools as soon as that page went up. So maybe there was a good reason for it once, but not anymore. – Ariel Aug 09 '11 at 20:22
  • @ariel some have been cleaned up, but there are still some bad ones. Since the link was to `js`. The first on the list for js is pretty bad. They only show that you can pass a string to setTimeout (which uses eval) and don't show how you can (and should) pass a function. It may be better than it was, but there are still issues. Even when they do create a function, they call it in a string. They promote bad practices. – James Montagne Aug 09 '11 at 20:28