Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Django: Include a template containing a script tag into another script tag

I want include a html file containing a script tag like this:
<script>$('#page-content').html('{% include "title.html" %}');</script>

title.html:
<script>document.title = "Hello World!"</script><p>Hello World!</p>

result:
<script>$('#page-content').html('<script>document.title = "Hello World!"</script><p>Hello World!</p>');</script>

Sadly, the browser won't execute it correctly due to the mulitple sets of script tags and I'm not quite sure how to solve it best

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well, just $.html will not execute scripts appended, but plain javascript can.

// const content = `{% include 'title.html' %}`;
const content = `<script>alert("foo")</script>`;
const target = document.getElementById('page-content');

const range = document.createRange();
range.selectNode(target);
const fragment = range.createContextualFragment(content);
target.appendChild(fragment);  // Here script is executed, "foo" pops up
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!