Javascript动态插入脚本元素

时间:2017-02-02 13:32:12

标签: javascript jquery

我想插入以下内容

buildscript {
    repositories {
        maven {
            url "${artifactoryUrl}/libs-release"
        }
    }
    dependencies {
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.4.10'
   }
}

subprojects {
    repositories {
        maven {
            url "${artifactoryUrl}/repo"
        }
    }

    artifactory {
        contextUrl = "${artifactoryUrl}"
        publish {
            repository {
                repoKey = 'libs-snapshot-local' // The Artifactory repository key to publish to
                username = "${artifactoryUser}" // The publisher user name
                password = "${artifactoryPassword}" // The publisher password
            }
        defaults {
                // Reference to Gradle publications defined in the build script.
                // This is how we tell the Artifactory Plugin which artifacts should be
                // published to Artifactory.
                publications('mavenJava')
                publishArtifacts = true
                // Properties to be attached to the published artifacts.
                properties = ['qa.level': 'basic', 'dev.team' : 'core']
            }
        }
        resolve {
            repoKey = 'repo'
        }
    }
}

我知道如何使用" src"属性。但就我而言,我想在脚本中添加一些变量x。

<script language="javascript">
var x = '-----BEGIN SOME-----\n
-----END SOME-----\n;';
</script>

2 个答案:

答案 0 :(得分:0)

   var script = document.createElement( 'script' );
   script.type = 'text/javascript';
   script.innerHTML = 'alert("Hey there... you just appended this script to the body");';
   $("body").append( script );

使用innerHTML向您的元素动态添加脚本。

答案 1 :(得分:0)

如果我正确理解你的意思是你想要生成一个带有变量的<script>标签。为此,您可以使用jQuery:

$('html').append('<script>console.log("hello world");</script>')

这将在控制台中显示hello world。当然,您可以将JavaScript代码切换为您想要的任何内容。