Pass Javascript to src link in async script - javascript

I'm trying to pass java script code to the script tag and it is being displayed as plain text. How should i correctly pass the object so that the value is populated correctly
var script = $('<script>', {
type: 'text/javascript',
src: 'mcbz.com/models?type=c43&geo=true&languageselected='+ window.location.pathname.substring(1,6)
});
script[0].setAttribute("async", "");
$('script:first').before(script);
Expected result
<script async src="mcbz.com/models?type=c43&geo=true&languageselected=en" ></script>

You can try this out.
const script = document.createElement("script");
script.type = "text/javascript";
script.src = `mcbz.com/models?type=c43&geo=true&languageselected=${ window.location.pathname.substring(1,6)}`;
script.setAttribute("async", "");
$("head").append(script);

Related

Execute a javascript from file on click button

I try to archive to execute a javascript from a file wish is located at assets/js/
but wen i click the button nothing happens.
I maybe do something wrong, i am new in javascript.
This is my code
<a href='linkhref.html' id='mylink'>view</a>
<script type="text/javascript">
var myLink = document.getElementById('mylink');
myLink.onclick = function(){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "assets/js/post.js";
document.getElementsByTagName("head")[0].appendChild(script);
return false;
}
</script>
Inicial i have to try to do like this code below
<a class ="faux-button" href=<?php HTML :: page_js("post.js");?> target="_black">View</a>
But no success to

Replace a string with a javascript code and load it (everything after pageload)

I'm hosting my blog/website on blogger, I need to insert an ad above any first chapter of any post. I can't modify any single post. I can't place the ad code in the theme html editor just before the post content, because I already have another ad before post content body.
I try with this 2 codes but none of them work:
1)
<script>
document.body.innerHTML = document.body.innerHTML.replace('<div class="description"', "<div id='toppostrect300x250' class='description'");
var container = document.getElementById('toppostrect300x250');
var w = document.write; document.write = function (content) {
container.innerHTML = content;
document.write = w; };
var script = document.createElement('script'); script.type = 'text/javascript'; script.src = '//p252740.clksite.com/adServe/banners?tid=252740_484260_6'; document.body.appendChild(script);
</script>
2)
<script>
document.body.innerHTML = document.body.innerHTML.replace('<h3>Chapter 1', "
<script data-cfasync='false' type='text/javascript' src='//p252740.clksite.com/adServe/banners?tid=252740_484260_6'><\/script>");
</script>

Append script tags in Jquery

I'm trying to append this in JS:
<script language='javascript'>document.pathXml='xml/infos.xml'</script>
this works: (except for the text within the "script" tag);
var script = document.createElement("script");
script.type = "javascript";
document.body.appendChild(script);
I just tried to do this to add the text :
var script = document.createElement("script");
script.type = "javascript";
$(this).html("document.pathXml='xml/infos.xml'");
document.body.appendChild(script);
but it's not working.
Any help is welcome!thanks in advance

Injecting javascript dynamically and using it

I also create a javascript file dynamically that uses those variables:
function callbackF(data){
console.log(data.script);
document.getElementsByTagName('head')[0].innerHTML=data.script;
var script = document.createElement("script");
script.setAttribute("src", "http://widget.example.com/sprk.1.0.2.js");
script.setAttribute("type", "text/javascript");
script.setAttribute("id", "grazit_script");
document.getElementsByTagName('head')[0].appendChild(script);
}
This is what i get in my head:
This is what is printed to the console log:
<script type='text/javascript'>var dbnwid=16476; var dbnpid=23369; var dbnwebid=19720; var dbnlayout=21; var dbncolor='#000000'; var dbntitlefontsize='14'; var dbnbgcolortype=1; var dbnheader='You might enjoy reading:'; var dbnremindercolor=2; var dbn_protocol = (('https:' == document.location.protocol) ? 'https://' : 'http://'); </script>
and then the script:
<script src="http://widget.example.com/sprk.1.0.2.js" type="text/javascript" id="grazit_script"></script>
The second script should get the variables that are in the second script..but it doesnt..then it complains about why it doesnt get those variables
UPDATE:
Both of those ways below didnt work for me:
eval(data.script);
var ss= new Function(data.script)();
Because scripts run when they are loaded. Adding a script tag using innerHTML doesn't run the code inside the tag.

inject js to head (with full-code in script)

I want to add a script to the head of a site so the the head of the target html looks like so <head><script type="text/javascript">*some code...*</script></head>.
With this script works that perfect:
var head = document.getElementsByTagName('head')[0],
script = document.createElement('script');
script.src = 'http://www.example.com/example.js';
head.appendChild(script);
But i don't want to use any link in source.
So i'm tried to add some code like this:
function addJS(jsCode) {
var styleElement = document.createElement('script');
styleElement.type = 'text/javascript';
(scriptElement.javascript) {
scriptElement.javascript.jsText = jsCode
scriptElement.appendChild(document.createTextNode(jsCode))
document.getElementsByTagName('head')[0].appendChild(scriptElement);
}
var jsCode = '';
jsCode += 'code';
jsCode += 'some more code';
But I've failed. That script is not working.
How can I add a Element to the head of any html site like this?
Would be great if someone could help me.
Just tried this and it seemed to work. Try
function addJS(jsCode) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.innerText = jsCode;
document.getElementsByTagName('head')[0].appendChild(s);
}
Demo here
Using jquery
var head = document.getElementsByTagName('head')[0],
script = document.createElement('script');
$(script).attr('type' , 'text/javascript');
head.appendChild(script);
$(script).append("alert('Hello')");

Categories

Resources