如何删除所有谷歌API相关的脚本?

时间:2016-09-25 18:17:22

标签: javascript google-maps google-maps-api-3

例如,我在google maps的标题中有许多脚本标记:

 https://maps.googleapis.com/maps-api-v3/api/js/26/5/common.js
 https://maps.googleapis.com/maps-api-v3/api/js/26/5/utils.js
 https://maps.googleapis.com/maps-api-v3/api/js/26/5/controls.js
 https://maps.googleapis.com/maps-api-v3/api/js/26/5/stats.js
 https://maps.googleapis.com/maps-api-v3/api/js/26/5/map.js 

我想按需编程删除这些。

1 个答案:

答案 0 :(得分:2)

示例1:如何通过src删除脚本(在这种情况下我们要删除google.js)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Listing 2.2</title>
    <script type="text/javascript" src="yahoo.js"></script>
    <script type="text/javascript" src="google.js"></script>
    <script type="text/javascript">
        var scripts = document.getElementsByTagName("script");
        for(var i = 0; i < scripts.length; i++) {
            if (scripts[i].src.match(/google/)) {
                scripts[i].parentNode.removeChild(scripts[i]);
            }
        }
    </script>
</head>
<body>
</body>
</html>

示例2:如何通过脚本src ID删除。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Listing 2.2</title>
    <script type="text/javascript" id="script-to-delete" src="yahoo.js"></script>
    <script type="text/javascript" src="google.js"></script>
    <script type="text/javascript">
        var scriptToDelete = document.getElementById("script-to-delete");
        scriptToDelete.parentNode.removeChild(scriptToDelete);
    </script>
</head>
<body>
</body>
</html>

希望这有助于你

新例子:

    <script type="text/javascript">
    var scripts = document.querySelectorAll("script[src*='maps.googleapis.com/maps-api-v3']");
    for(var i = 0; i < scripts.length; i++) {
        scripts[i].parentNode.removeChild(scripts[i]);
    }
</script>