I have a template in django called base.html that is calling another template through the {% include 'gui/page.html' %}
in that template I have the following javascript which allows the ability to select an an entire text field when setup like <p>This text I will select all</p>
<script>
$(document).ready(function(){
$('p').dblclick(function(e){
$(this).selectText();
e.preventDefault();
});
});
jQuery.fn.selectText = function(){
this.find('input').each(function() {
if($(this).prev().length == 0 || !$(this).prev().hasClass('p_copy')) {
$('<p class="p_copy" style="position: absolute; z-index: -1;"></p>').insertBefore($(this));
}
$(this).prev().html($(this).val());
});
var doc = document;
var element = this[0];
console.log(this, element);
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
};
</script>
If I access the template directly @ http://localhost/gui/page.html the javascript works, but if I access it through http://localhost/base.html double clicking on the field does nothing.
I've tried including the javascript in the base.html and it still doesn't load. How does django load javascript when calling through an included template?
gui/page.htmlare being included when you openbase.html? Have you done view source in your browser to make sure that the javascript is there? Based on the URLs that you are using it appears that Apache (or your other server) is statically serving the filebase.htmlrather than allowing django to serve the file in which case the template is not being processed and not includinggui/page.html. How is your web server set up?<script></script>tags and put the contents into ajs/scripts.jsfile and it works.python manage.py runserverfrom your django project directory or your mod_wsgi or gunicorn to run your django project. Are you using any of those?