I have a form and I need to run some javascript to parse the page info stored in the form to open it.
<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["fname"].value
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
function open()
{
if (validateForm()) {
1. get the value
2. parse the value to get year/month/date (?)
3. compose the string of webpage (?)
4. open the webpage (?)
}
}
</script>
<form name="myForm" onsubmit="return open()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
The input is of the format 2011/03/05 and I need to open http://abc/def/2011/03/2011_03_05.html. It requires parsing the date, and append string, and open the page.
ANSWER
<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
return true;
}
function openPage()
{
if (validateForm()) {
var value = document.forms["myForm"]["fname"].value
var strYear = value.substring(0,4);
var strMonth = value.substring(5,7);
var strDay = value.substring(8,10);
var strURL = "http://abc/def/"+strYear+"/"+strMonth+"/"+strYear+"_"+strMonth+"_"+strDay+".html";
alert("strURL");
//document.location.replace(strURL)
//document.write(strURL);
window.open(strURL,"myWindow");
}
}
</script>
<form name="myForm" onsubmit="openPage()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
Assuming that the input is always of the form yyyy/mm/dd, you can parse the string as follows: