Chris
Chris

Reputation: 25

Trying to replace the contents of a <script> tag using Javascript

Our Company uses a closed sourced shopping cart system for its e-comreace web site. The check out page is generated by a cgi script that I can not modify. I would like to replace the form validation script it generates with my own. The script I am trying to replace is the second script in the document. I have inserted the following script in the footer section:

document.body.getElementsByTagName("SCRIPT")[1].innerHTML = "New Validation Function Here";

however when the validation function is called the old one executes, not my new function.

Where am I going wrong? Is there a better way?

Chris

Upvotes: 0

Views: 785

Answers (2)

Sebastián Grignoli
Sebastián Grignoli

Reputation: 33432

You don't need to replace the old SCRIPT tag that was already executed.

The validation function is just a value, and the function name is a variable, so you could just do this:

<script>

oldValidationFunctionName = function(param1, param2, whatever) {
    // the new function body here...
 }

</script>

Upvotes: 1

Igor Zevaka
Igor Zevaka

Reputation: 76500

You probably have a better chance at replacing the validation function in the global scope.

<!-- Old script file -->
<script>

function doValidate(value) {
 return true;
}
</script>

<!-- New script file -->
<script>
function doValidateNew(value) {
 return false;
}
window.doValidate = doValidateNew;
</script>

Upvotes: 5

Related Questions