/    Sign up×
Community /Pin to ProfileBookmark

Hello.

I have a form which in the url of the page, displays variable information after a “?” I have javascript which captures the URL variable info and passes it to a hidden feild. I need to have a radio button which allows users to go to another iteration of of this form. The problem is, when users clicks the radio button and gets to the other form, none of the URL variables display since it is not part of the onclick=”location command.
Is there a way to have the second page display the first page’s URL variables after the ‘?’ ?

Any help is much appreciated.

I’ve tried the script below but does not work:

function getParams() {
var idx = document.URL.indexOf(‘?’);
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split(‘&’);
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split(‘=’);
params[nameVal[0]] = nameVal[1];
}
}
return params;
}

<body>
<SCRIPT LANGUAGE=”JavaScript”><!–
dealer_id = unescape(params[‘dealer_id’]);
document.write(‘<input name=”radiobutton” type=”radio” value=”radiobutton” onclick=”location=’consumer_contact.html?dealer_id=”‘ + dealer_id + ””>’);
//–></SCRIPT>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 22.2006 — To start with, you can replace your function with this improvement of it:
function getParams() {
var x, str = self.location.search.substr(1);
var params = new Array();
if (str) {
var pairs = str.split('&amp;');
for (x=0; x&lt;pairs.length; x++) {
nameVal = pairs[x].replace(/+/g, " ").split('=');
params[unescape(nameVal[0])] = unescape(nameVal[1]);
}
}
return params;
}

Then, instead of doing this:
[code=html]<SCRIPT LANGUAGE="JavaScript"><!--
dealer_id = unescape(params['dealer_id']);
document.write('<input name="radiobutton" type="radio" value="radiobutton"
onclick="location='consumer_contact.html?dealer_id="' + dealer_id + ''">');
//--></SCRIPT>[/code]

You could just do this:
[code=html]<SCRIPT LANGUAGE="JavaScript"><!--//
document.write('<input name="radiobutton" type="radio" value="radiobutton"
onclick="self.location.href='consumer_contact.html', self.location.search, '';">');
//--></SCRIPT>[/code]
Copy linkTweet thisAlerts:
@elradauthorJun 23.2006 — Thanks, but the radio button does not display on the page. Is there a way to display the radio button without loosing this functionality?
Copy linkTweet thisAlerts:
@QuerelJun 23.2006 — I don't exactly understand what you are trying to do, but I think you will find at least some ideas in this code:

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;

function parseRadioButtons() {
var params = getParams();
dealer_id = unescape(params['dealer_id']);
for(var i=0;i&lt;3;i++) {
document.write('&lt;input ');
if(dealer_id==i) {
document.write('checked ');
}
document.write('name="radiobutton" type="radio" value="radiobutton" onclick="redirect(' + i + ');"/&gt;' + i + '&lt;br/&gt;');
}
}

function getParams() {
var x, str = self.location.search.substr(1);
var params = new Array();
if (str) {
var pairs = str.split('&amp;');
for (x=0; x&lt;pairs.length; x++) {
nameVal = pairs[x].replace(/+/g, " ").split('=');
params[unescape(nameVal[0])] = unescape(nameVal[1]);
}
}
return params;
}

function redirect(value) {
var parameterName = "dealer_id";

<i> </i>var parameters = self.location.search;
<i> </i>var newURL = self.location.pathname;

<i> </i>parameterName = parameterName + "=";
<i> </i>var parameterLocation = parameters.indexOf(parameterName);

<i> </i>//Check if the parameter specified allready exists in the URL
<i> </i>if(parameterLocation!=-1) {
<i> </i> //It exists, so we should alter the value, the rest can stay
<i> </i> var equalsSignLocation = parameterLocation + parameterName.length;

<i> </i> //Just copy everything to where the parameters value begin
<i> </i> //Add our new value to it also
<i> </i> newURL = newURL + parameters.substr(0,equalsSignLocation) + value;

<i> </i> var ampersandLocation = parameters.indexOf("&amp;",equalsSignLocation);
<i> </i> if(ampersandLocation!=-1) {
<i> </i> //If there is something after the value it should be added as well
<i> </i> newURL = newURL + parameters.substr(ampersandLocation);
<i> </i> }

<i> </i>} else {
<i> </i> //The parameter is not specified
<i> </i> //Add the current parameters
<i> </i> newURL = newURL + parameters;
<i> </i> //There is no question mark, so add it
<i> </i> if(parameters=="") {
<i> </i> newURL = newURL + "?";
<i> </i> } else {
<i> </i> var lastChar = parameters.charAt(parameters.length-1);
<i> </i> //Check if there is allready an ampersand or an question mark
<i> </i> if(lastChar != '&amp;' &amp;&amp; lastChar != '?') {
<i> </i> //If not, add one
<i> </i> newURL = newURL + "&amp;";
<i> </i> }
<i> </i> }
<i> </i> //add the parameter
<i> </i> newURL = newURL + parameterName + value;

<i> </i>}

<i> </i>self.location = newURL;
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script&gt;
parseRadioButtons();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @elrad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...