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

151
Views
About JavaScript: Can I modify the contents of a script tag after the js text contained in it is loaded and before it is executed?

For example, I have a <script> alert(1) <script> in the web page. Now what I want to do is to change it to <script> alert (2) <script> after it is loaded and before it is executed, immediately. Can I succeed?

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

0

The short answer is no; only by modifying the page before it is served

As you can see, you cannot intercept the script with script

It will be executed when parsed

window.addEventListener("DOMContentLoaded",function() {
  document.scripts[1].textContent = "";
  alert(2)
})
<script>alert(1)</script>

Here is a hack

window.addEventListener("DOMContentLoaded",function() {
  alert(3)
})
<script>// if you can insert this before you can intercept alert
const saveAlert = window.alert;
window.alert = function(str) {
  saveAlert(2)
  window.alert = saveAlert;
};
</script>
<script>alert(1)</script>

Proof that the other answer does not work

var h = document.getElementById('hello');
h.textContent = 'alert(2)'
<script type='text/javascript' id='hello'>
    alert(1)
</script>

about 4 years ago · Juan Pablo Isaza Report

0

Say you set your script tag like so

<script type='text/javascript' id='hello'>
    alert(1)
</script>

Then you can get the dom element like so:

var h = document.getElementById('hello');

You can then modify the element like so:

h.innerHTML = 'alert(2)'

I would suggest also that you use something like JQuery onready to make sure that the modification code executes before the alert in the script tags.

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!