I want to disable the script tag or if its possible to change script src
or type
in this script using javascript?
<script type="text/javascript" src="./1.js.download"></script>
I can't add id
in this code.
please help and feel free to ask anything if you don't understand
CodePudding user response:
How about removing it altogether? The example is a simple function that get an element by the given selector
and remove it. FYI if you don't want the <script>
to actually load, then your'e out of luck. It'll be parsed as long as it's in the HTML, there's no way to avoid it from loading by JavaScript.
In the example there are 3 <script>
tags and the target is in the middle. document.querySelector("script")
will stop at the first match so you'll need a more specific selector. To target the second tag I used script:nth-of-type(2)
. You can verify success using by F12
const killTag = selector => document.querySelector(selector).remove();
killTag(`script:nth-of-type(2)`);
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="./1.js.download"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
CodePudding user response:
You can remove src from script tag like this:
s = document.querySelectorAll('script')
console.log(s[1])
s[1].setAttribute('src', '')
console.log(s[1])
<script type="text/javascript" src="./1.js.download"></script>
You will have maybe multiple scripts in your dom. then you have take a look which element it is and change the key from the collection. in this case the key was 1.
CodePudding user response:
If you need to select specific script
element you can get it by attribute like this:
let ok = document.querySelector('[src="./1.js.download"]');
//Then you can either change id or type:
ok.setAttribute('id', 'ok');
ok.setAttribute('type', 'module');