Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:15:38+00:00 2026-06-08T09:15:38+00:00

How can I remove script elements before they are being executed ? I thought

  • 0

How can I remove script elements before they are being executed?

I thought about using the DOMNodeInserted event, but apparently it doesn’t catch script elements. I’ve also tried using the jQuery livequery plugin like that:

$("script").livequery(function () { 
    $(this).remove();
});

It did remove the script element, but after it was executed.

I’m looking for a cross-browser solution, but I’m not even sure if that’s possible. I read about Mutation Observers which seems close enough but I’m not sure if it can solve my problem.

It would be even better if there was a way to modify the script content before it is being executed without removing and recreating it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:15:40+00:00Added an answer on June 8, 2026 at 9:15 am

    Removing a script element does not do anything. If you can somehow access a script element, it was executed a long time ago and removing it will have no effect.

    So we need to work around it. If your script element is at the top of the page like this:

    <head>
        <script src="yourscript.js"></script>
    

    You could make a synchronous ajax request to the same page, so you can parse its content into a new document, modify all script tags and then replace
    the current document with the modified document.

    var xhr = new XMLHttpRequest,
        content,
        doc,
        scripts;
    
    xhr.open( "GET", document.URL, false );
    xhr.send(null);
    content = xhr.responseText;
    
    doc = document.implementation.createHTMLDocument(""+(document.title || ""));
    
    doc.open();
    doc.write(content);
    doc.close();
    
    
    scripts = doc.getElementsByTagName("script");
    //Modify scripts as you please
    [].forEach.call( scripts, function( script ) {
        script.removeAttribute("src");
        script.innerHTML = 'alert("hello world");';
    });
    
    //Doing this will activate all the modified scripts and the "old page" will be gone as the document is replaced
    document.replaceChild( document.importNode(doc.documentElement, true), document.documentElement);
    

    Unfortunately this cannot be set up in jsfiddle or jsbin. But you should be able to copy paste this code exactly as it is into this
    page’s console in google chrome. You should see the alerts and when you inspect the live dom, each script was modified.

    The difference is that we are running this after scripts have been executed on the page, so the old scripts should still have a working effect on the page.
    That’s why, for this to work, you need to be the very first script on the page to do it.

    Tested to work in google chrome. Firefox is completely ignoring the doc.write call for some reason.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove all script elements from a HTML page. But for
How can I remove elements which are created by JavaScript, I tried using CSS
we can remove formula from one cell by cell.setCellFormula(null) . but if i want
One can remove all calls to printf() using #define printf . What if I
I was wondering if when using the datagridview control you can remove the thing
how can I remove my appended script because it causes some problems in my
I have a script (helped out by @briguy37 Remove multiple elements with same name
I have problem I can't figure out but I do have a hint. Before
I have a script to clear all fields but won't remove all the style
How we can remove an array entry from : var a = [],b =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.