I have two html files (for add and edit) which contains js codes. These two files are identical except few js lines in its functions. Actually I don’t like to have common code for both files. Is there a good way to handle this kind of situations ?
Example:
(file one)
<html>
<title>title goes here</title>
<javascript>
$('#button1').click(function () {
$.ajax({
type: "POST",
url: '/admin/test/checkcode',
data: {code:code, id:id}, // in here there is an id
async: false,
success: function(data){
}
});
}
</script>
<body>
</body>
</html>
(file two)
<html>
<title>title goes here</title>
<javascript>
$('#button1').click(function () {
$.ajax({
type: "POST",
url: '/admin/test/checkcode',
data: {code:code}, // in here there is no id
async: false,
success: function(data){
}
});
}
</script>
<body>
</body>
</html>
You may want to try putting that JavaScript code into a separate file. That way, you can just include the single JavaScript file into each of those pages… and only have to make changes to that one JavaScript file.
For reference, here is a way to include a JavaScript file:
Inside of that JavaScript file, there could be some type of basic function that takes parameters for that
datavalue that is used in the$.ajax()call. For example, something like: