/    Sign up×
Community /Pin to ProfileBookmark

Using URL parameters in HTML

I have a query string which contains various elements…

&Var1=10.00
&Var2=5.00

etc.,

and I’d like to reference these values within my html code (multiple times).

I’ve tried embedding <script> javascript tags into the html, but this doesn’t work. I.e.

<script>var MyVar1= getElementById(‘Var1’).value;document.write MyVar1;</script>

I can obviously refer to the element once by putting within an input form element, but I want to refer to the variable mutiple times.

I hope this makes sense!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeFeb 11.2011 — <i>
</i>var thisURL = document.URL;
var thisNVP = new Array();
thisNVP = thisURL.split("?"); //This grabs all URL parameters
var nvpArray = thisNVP[1];
nvpArray = thisNVP.split("&amp;"); //This puts each name/value pair into an array
nvpLen = nvpArray.length;
for(i=0;i&lt;nvpLen;i++) {
document.write(nvpArray[0]+" = "+nvpArray[1]+"&lt;br /&gt;");
}


^_^
Copy linkTweet thisAlerts:
@here2learnFeb 11.2011 — you can use request object

[CODE]<script>
var MyVar1="<%=request.getParameter("Var1")%>";
</script>[/CODE]


then use MyVar1 in document.write as you asked
Copy linkTweet thisAlerts:
@WolfShadeFeb 11.2011 — If he's using ASP, yes. Or any dynamic scripting language (PHP, JSP, ColdFusion, etc.). But if it's just HTML, no.

^_^
Copy linkTweet thisAlerts:
@DiamondDog8authorFeb 11.2011 — I'm using just HTML so I can't access the variables like that. I have the javascript code to split out the elements in the URL as I've said, but I need to know how to reference these in the code multiple times.

I know that I can use

<input> to contain the variable name once, but I need to be able to reference it several times on the same page.
Copy linkTweet thisAlerts:
@WolfShadeFeb 11.2011 — Ah! You just mentioned a querystring; I didn't realize you already had it split. In that case, you just reference the array index (zero-based, of course), so from your two var example, array[0] would be "var1=10.00", and array[1] would be "var2=5.00". If you really needed to separate the name from the value and keep them associated, split them one more time within a loop (as I have in my sample code) and use eval() (as much as I dislike eval) to set newArray[0] as the variable name and newArray[1] as its associated value.

eval("var "+newArray[0]+" = "+newArray[1]+"; ");

^_^
Copy linkTweet thisAlerts:
@mirza_yasir4Feb 12.2011 — I think the problem is fixed now...................
×

Success!

Help @DiamondDog8 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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