I have some old code with document.write in it.
<script type="text/javascript" language="JavaScript1.2">
document.write('<form name="InvGenPayDonation" action="'+PostURL+'" onsubmit="return validateForm();" method="POST">');
</script>
How can I replace action with a javascript variable that has been determined when the page was loaded? Here is that code, which is not part of a function. (I’ve substituted names, so the substr indexes are not correct, but I’m sure you get the point.)
// Check where we came from so that we can go to the right spot when the
// form gets posted from outside of our HTML tree.
var PostURL = '/event.php';
if (window.location.href.substr(0, 21) == 'http://10.10.10.10:80')
PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 14) == 'http://webapps')
PostURL = 'http://10.100.0.11:8580/event.php';
else if (window.location.href.substr(0, 7) == 'webapps')
PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 10) == 'http://www')
PostURL = 'https://secure.french.toast.new_zeland.france/event.php';
I’d like PostURL to go into the form’s action. If I can do that without document.write, that would be great. I just need some help. Thanks.
Some other DOM0 code should do the trick:
Note that this assumes that your form is the first form on the page – if it is not, replace
[0]with the zero-based index of the form.However, you can (and probably should) use a more modern solution that is not dependent on the layout of the page. The simplest is
document.getElementById– simply add an ID attribute to your form (make sure the ID’s value is unique across the entire page) and then do the following: