-3

My webpage is including a .js file whose functionality I have to change/replace/expand.

I have the new JS all written out and included in my webpage, but I can't get it to overwrite the original script or prevent the original script from executing.

Assume I am unable to remove the original <script> element from the page, but I am able to include my new script before the original script.

EDIT: Here's how it's laid out:

<script src="my-new-script.js"></script>

<!-- Some HTML -->

<!-- I want to replace the functionality of the below script with 
"my-new-script.js" and/or prevent my-old-script.js from executing -->

<script src="my-old-script.js"></script>

The reason: my-old-script.js is being loaded by a WordPress theme, and for my purposes I cannot change the theme or alter PHP files of the theme, so I cannot prevent my-old-script.js from being loaded. But, I can include my-new-script.js via Widgets

I looked at some of the answers given here, but they didn't seem to apply to my scenario

4
  • Show relevant code to explain your issue more. Commented Jan 20, 2021 at 19:05
  • How is it possible that you can add your script but not remove the other script? Commented Jan 20, 2021 at 19:06
  • If you're including it before the original script, then it will be overwritten by that script... Commented Jan 20, 2021 at 19:06
  • So I answered this 2 hours ago. Did you look at my script? Commented Jan 20, 2021 at 21:14

1 Answer 1

1

So I could not remove the script because it's functions were already saved in the window scope, but I could re-assign the function

<!doctype html>
<html>

<head>
  <title>Test</title>
  <script>
    window.addEventListener("load", function() {
      //  document.querySelectorAll("script")[1].remove()
      window.myFunc = () => {
        console.log("my first func")
      }
    })
  </script>
  <script>
    function myFunc() {
      console.log("my second func")
    }
  </script>
</head>

<body>
  <button type="button" onclick="myFunc()">Click</button>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.