PDA

View Full Version : JS Displaying passed variable in HTML



Pherank
July 19th, 2009, 12:29 AM
I've forgotten how to do this, so I have to ask the newbie question:
How do I get JS to print a variable passed between pages?

First page link:


<p style="font-weight:bold;"><a href="event_registration.html?EventName=Vallejo">Register for this Event</a></p>
Receiving page:


<h1><script language="JavaScript1.2" type="text/javascript">document.write('$EventName' + " ");</script>Event Registration</h1>

Swooter
July 19th, 2009, 06:09 AM
function getParameter($name) {
return window.location.href.split($name + "=")[1].split("&")[0];
}
var EventName = getParameter("EventName");

Pherank
July 19th, 2009, 03:19 PM
Thanks Swooter! I also ran into this explanation online (for anyone else interested):

http://www.idealog.us/2006/06/javascript_to_p.html

Pherank
July 19th, 2009, 04:37 PM
Here's a related question: Can the query string variable be placed into a hidden form field? This type of thing:



<input type="hidden" name="EventName" id="EventName" value="document.write($EventName));" />


Example doesn't work of course, but hopefully you get the concept...

Swooter
July 26th, 2009, 07:40 AM
You'll need to make a onload function such as:
<script type="text/javascript">
window.onload = function() {
var hiddenField = document.createElement("INPUT");
hiddenField.type="hidden";
hiddenField.value=getParameter('someVariable');
document.getElementById("myForm").appendChild(hiddenField);
}
</script>
<form method="POST" id="myForm">

</form>