views:

668

answers:

3

How can I place document.location.search into a variable w/o the '?' Is there a simple regex, or can I just ignore the first character?

A: 

You can simply replace the "?" character with an empty string:

var searchWithoutQuestionMark = document.location.search.replace('?', '');

Steve

Steve Harrison
+6  A: 

No problem

window.location.search.substr(1);

Edit

I didn't even think about it the first time, but you should refer to window.location, not document.location. It has the broadest browser support.

https://developer.mozilla.org/En/Document.location#Notes

Peter Bailey
Indeed the best solution. Mine was unnecessarily complicated, so I have deleted my answer.
Cerebrus
String.substr is deprecated, instead use String.substring or String.slice
J-P
It is? Don't see a mention here https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/substr or here http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/guide/obj.html#1008731Where can I find this info?
Peter Bailey
Is it a given (and compatible amongst all modern browsers) that location.search will begin with a question mark?
eyelidlessness
A: 

Hi all, i have

<script language="javascript">
document.write("<a href='http://www.gfi.com/downloads/downloads.aspx?pid=fax&amp;lid=en" +      document.location.search + "'><img src='http://images.gfi.com/download-imagery/button-  download.png' border='0'></a>");
  </script>

I want to replace the ? with &... How i can do that please?

Rene Zammit