I have now all my javascript code in my views (PHP). But I want to seperate that (for deleting duplicate code). I am currently using CodeIgniter framework and currently I use functions like this :
$(function() {
$("#serialNumber").autocomplete({
source: "<?php echo site_url('manage/getSerials'); ?>",
change: function(event, ui) {
if (!ui.item) {
$(event.target).val("");
}
},
focus: function(event, ui) {
return false;
}
});
});
However you can't use javascript variables inside php.
So should I create for each link a variable and use it like that in my javascript or use the javascript in a seperate php file or is there a better way around it ?
The answer with help from ToniTornado :
First make a function like this :
function getURL(link) {
var site = "<?php echo site_url(); ?>";
site += "/" + link;
return site;
}
Include that in your collection where all your jquery, css files are Then use the function like this (jQuery) :
$(function() {
$("#serialNumber").autocomplete({
source: getURL('controller/function'),
change: function(event, ui) {
if (!ui.item) {
$(event.target).val("");
}
},
focus: function(event, ui) {
return false;
}
});
});