I need to create a script element as below,
<div class="abcd">
<script type="text/javascript">
var a = "10";
var b = "20";
(function(d) {
//lines of code of the function
}(document));
</script>
</div>
I have tried to complete these so by first creating a script element by document.Createelement option. and adding this script part into the div. But not able to add the code part within the script part.
Need to add both var code part and also the function.
Please help.
Thanks in advance
EDIT ----
I am using below code now,
var headtg = document.getElementsByTagName('head')[0];
var divElm = document.createElement('div');
var scpt = document.createElement('script');
scpt.type = 'text/javascript';
var clName= "TEST";
var myfunc = function innercode(i) {
var a = i;
var b = a;
(function (d) {
//codes
}(document));
}
divElm.className = "ABC_" + clName;
divElm.appendChild(scpt);
headtg.appendChild(divElm);
scpt.innerHTML = myfunc;
But the output coming up as
<div class="abcd">
<script type="text/javascript">
function innercode(zID){var a = "10";var b = "20";;
(function(d) {
//lines of code of the function
}(document));
</script>
</div>
But I want the output to be like,
<div class="abcd">
<script type="text/javascript">
var a = "10";
var b = "20";;
(function(d) {
//lines of code of the function
}(document));
</script>
</div>
Please advise
You could just get the div element you want the code to be in, and then set the innerhtml to <script>your code here</script>
EDIT: Try assigning your function like so:
var myFunc = function() {
//Code
}
Then you can call myFunc(parameters)
You can write your code like this :
<html>
<head>
<script>
var a = "10";
var b = "20";
function //here write you function name(d) {
//lines of code of the function
};
</script>
</head>
<body>
</body>
</html>
Or you can write at this way :
<html>
<body onload='write your javascript code here'>
</body>
</html>
i hope it will work.let me know.
i understand from you comment on my persevios answer you want to writer this code :
var headtg = document.getElementsByTagName('head')[0];
var divElm = document.createElement('div');
var scpt = document.createElement('script');
scpt.type = 'text/javascript';
var clName= "TEST";
divElm.className = "ABC_" + clName; divElm.appendChild(scpt);
headtg.appendChild(divElm);
function innercode(i) {
var a = i; var b = a;
}
function (d) {
//codes }(document));
}
if it is true you can write like this :
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" charset="utf-8" >
var headtg = document.getElementsByTagName('head')[0];
var divElm = document.createElement('div');
var scpt = document.createElement('script');
scpt.type = 'text/javascript';
var clName= "TEST";
divElm.className = "ABC_" + clName; divElm.appendChild(scpt);
headtg.appendChild(divElm);
function innercode(i) {
var a = i; var b = a;
}
function (d) {
//codes }(document));
}
</script>
</head>
<body>
</body>
</html>
i hope it will work.let me know
来源:https://stackoverflow.com/questions/44584931/how-to-add-local-variables-and-a-different-function-in-script-element-in-javascr