You need to enable JavaScript to run this app. 标签,提供使用JavaScript禁用该标签或修改其src、type属性的实用方案,满足HTML script标签的动态操作需求。", "datePublished": "2026-05-06T06:51:54.000Z", "author": { "@type": "Person", "name": "数据老陈" }, "publisher": { "@id": "https://www.volcengine.com/#organization" }, "isPartOf": { "@id": "https://www.volcengine.com/#website" }, "keywords": "JavaScript禁用script标签, 修改script标签src, 修改script标签type, 无ID操作script标签, HTML script标签处理", "inLanguage": "zh-CN" }, { "@type": "Organization", "@id": "https://www.volcengine.com/#organization", "name": "火山引擎", "url": "https://www.volcengine.com/", "logo": { "@type": "ImageObject", "url": "https://lf3-static.bytednsdoc.com/obj/eden-cn/shayvw_lmjsvc/ljhwZthlaukjlkulzlp/logo-theme-light.svg" } }, { "@type": "WebSite", "@id": "https://www.volcengine.com/#website", "url": "https://www.volcengine.com/", "name": "火山引擎", "publisher": { "@id": "https://www.volcengine.com/#organization" } } ] }
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript禁用HTML中的script标签或修改其src/type属性(无法添加ID场景)

Got it, let's work through this problem step by step. Since we can't add an ID to that specific script tag, we'll target it using its existing type and src attributes—here are reliable approaches for each of your goals:

1. Disable the Script (Prevent Execution)

The easiest way to disable a script without removing it is to change its type to a value browsers don't recognize as executable (like text/plain). This tells the browser to ignore the script instead of running it. You can also remove the script entirely if it hasn't loaded yet.

// Target the script by matching its exact type and src attributes
const targetScript = document.querySelector('script[type="text/javascript"][src="./1.js.download"]');

if (targetScript) {
  // Option 1: Change type to non-executable format
  targetScript.type = 'text/plain';
  
  // Option 2: Remove the script from the DOM (only works if it hasn't executed yet)
  // targetScript.remove();
}

Critical Note: This code must run before the target script loads or executes. Place your script in the <head> section (before the target script) or directly immediately above the target script tag.

2. Modify the src Attribute

If you want to swap out the script file entirely, target the tag and update its src property:

const targetScript = document.querySelector('script[type="text/javascript"][src="./1.js.download"]');

if (targetScript) {
  // Replace with your desired script URL
  targetScript.src = './your-custom-script.js';
}

Again, timing matters here—modify the src before the browser starts downloading the original script.

3. Modify the type Attribute

If you need to change the script's execution type (instead of disabling it), you can update the type to valid values like module (for ES modules) or text/babel (if using Babel, assuming you have the necessary loader):

const targetScript = document.querySelector('script[type="text/javascript"][src="./1.js.download"]');

if (targetScript) {
  // Change to ES module type, for example
  targetScript.type = 'module';
}
Handling Dynamically Loaded Scripts

If the target script is added to the DOM after page load (dynamically), use a MutationObserver to catch it as soon as it's inserted and modify it before it runs:

// Set up observer to watch for new nodes in the DOM
const observer = new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    // Check each newly added node
    const targetScript = Array.from(mutation.addedNodes).find(node => 
      node.tagName === 'SCRIPT' && 
      node.type === 'text/javascript' && 
      node.src === './1.js.download'
    );

    if (targetScript) {
      // Perform your action (disable, modify src/type)
      targetScript.type = 'text/plain';
      // Stop observing once we've found and modified the script
      observer.disconnect();
    }
  });
});

// Start observing the entire body for changes
observer.observe(document.body, { childList: true, subtree: true });

This observer will watch for any new nodes added to the DOM, find the target script, and apply your changes immediately—before it has a chance to execute.

内容的提问来源于stack exchange,提问作者user15307792

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:51:53