简体   繁体   中英

Changing Javascript within an HTML document to an external Javascript file

This Javascript Function..

function ClearTotals() {  

    document.getElementById("total1").value = "";
    document.getElementById("total2").value = "";
}

sets resets the values in two form fields. However when I move this function, unchanged, to an external Javascript and reference it in the HTML page as below:

<script src="http://xxxxxxx.org/pkjs/js1.js/"></script>

it doesn't work.

Do I need to pass some reference to the document which is used in the function, or is there some other reason why this doesn't work. Thanks

The problem is js1.js - as the error message made clear ! For some reason only the first function was being read even though I can see no problems with the structure of the file. If I delete all the other functions, and test the function by typing the url in the browser, I see the javascript code for ClearTotals().

And when I submit the HTML form, the field values clear as they should. That's really good - there are no issues with putting internal js code into an external file. Thanks again to everyone for their input and apologies that the problem seems down to me !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM