Forum Moderators: open

Message Too Old, No Replies

add querystring with javascript

         

jackvull

11:04 am on Jan 30, 2006 (gmt 0)

10+ Year Member



Hi
I am trying to reload the page as soon as an option box is selected. Below is the code so far:

<select name='currencychoice' style='background-color:#FFFF99; width:50px;' onChange='
var queryString = "cc=" + options[selectedIndex].value;
window.location.href = window.location.href + queryString'>

The problem I have is identifying whether the page already has a querystring.
- if the? is already there, e.g. index.php?, then I don't want to add it;
- if there is already a querystring present then I need to add an & symbol before the cc, e.g. index.php?page=1&cc=1
- otherwise, if there is no querystring then I need to add in the?, e.g. index.php?cc=1

Is there an easier way of doing this and reloading the page with the selected option?
I've tried window.location.href = options[selectedIndex].value
but that seems to strip off the actual PHP page from the URL

Thanks.

Bernard Marx

12:41 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very basic this one. Any use?

[pre]


<script type="text/javascript">

function relocate(select,attribName)
{
var addQuery = select.options[select.selectedIndex].value;
if(!addQuery) return; /* no selection */
var currQuery = location.search.substring(1);
location.href += (currQuery?"&":"?") + attribName+"="+addQuery;
}
</script>
</head>
<body>
<select onchange="relocate(this,'cc')">
<option value="">Choose..
<option value="1">1
<option value="2">2
<option value="3">3
</select>

</body>[/pre]

jackvull

1:15 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



Perfect. Thanks.