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

303
Views
Inject and execute JavaScript into an existing DOM

I'm playing with inject JavaScript code into an existing DOM.

I've seen that if I put the script by hand in the browser's DOM elements inspector

<script>alert("XSS test");</script>

the script node is added to the DOM, but it has no effect. I mean, no alert box is shown.

screenshot

On the other hand, if I put the script as plain text through the JavaScript console using document.write(), in this case the code got parsed and executed immediately, and the alert box is shown as expected.

document.write('<script>alert("XSS test");</script>');

I'm used to see that hand made changes to the DOM elements in the inspector reflects immediately in the page, while it seems that JavaScript nodes are an exception.

Why web browsers do not execute JavaScript nodes put by hand in the inspector?

Is there another way to send the script node to the DOM and make it run immediately?

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

0

script tags added via innerHTML and related methods (insertAdjacentHTML, etc.) are not executed. (The exact rules are somewhere in the script portion of the HTML specification but it's heavy going.) This may be because early on it was identified that poorly-written pages might use innerHTML and such to append user content, and so not executing it was a quick and simple way to avoid very, very basic XSS attacks. But only very, very basic ones.

You can add a script tag to the DOM and have it executed by creating and appending a script tag:

const script = document.createElement("script");
script.textContent = `console.log("Hi there");`;
document.body.appendChild(script);

You can do that from the console tab in devtools, for instance, rather than the DOM inspector tab.

about 4 years ago · Juan Pablo Isaza Report

0

try going to the web page you want and then in the URL bar at the top add

javascript:[your script]

for example

javascript:alert("Hello World!");

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!