I have some javascript that checks the url is "musicmartian.com" and will then run a script that displays a pupunder ad, but doesnt seem to work.
the code is:
<script type="text/javascript">
if (window.location.href=="musicmartian.com") {
src='//go.oclaserver.com/apu.php?zoneid=540338'
}
</script>
The website is: http://musicmartian.com/ and the ad provider is PropellorAds if thats needed information.
Insert this code inside body of your html (before </body>)
<script type="text/javascript">
if (window.location.hostname === "musicmartian.com") {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '//go.oclaserver.com/apu.php?zoneid=540338';
document.body.appendChild(script);
}
</script>
Related
I want to load a constants file as per the language the user has selected. For that I want to load the scripts dynamically.
<html>
<body>
<script type="text/javascript">
var jsElement = document.createElement("script");
jsElement.type = "application/javascript";
jsElement.src = "../constants.en.js";
document.body.appendChild(jsElement);
</script>
<script type="text/javascript" src="../js/RandomScript.js"></script>
</body>
Whole code is in HTML.
When I tried the code above, RandomScript.js is loaded before the constant file.
How can I maintain sequence of loading files.
I am not using jQuery or something, so is there any way to do interpolation of src of script?
You can use onload event listener to load .js files sequentially.
First create an array of URLs of scripts. Then loop through it recursively. onload event listener ensures that the scripts are loaded sequentially.
var scriptURLs = [
"../constants.en.js",
"../js/RandomScript.js"
];
function loadScript(index){
if(index >= scriptURLs.length){
return false;
}
var el = document.createElement('script');
el.onload = function(){
console.log("Script loaded: ", scriptURLs[index]);
loadScript(index+1);
}
el.src = scriptURLs[index];
document.body.appendChild(el);
// OR
// document.head.appendChild(el);
}
loadScript(0); // Load the first script manually.
Hope this helps.
var jsElement = document.createElement("script");
jsElement.type = "application/javascript";
document.body.appendChild(jsElement);
jsElement.onload = () => {
callyournewScript();
}
jsElement.src = "../constants.en.js";
Load the ../js/RandomScript.js after loading the ../constants.en.js like following
<body>
<script type="text/javascript">
var jsElement = document.createElement("script");
jsElement.type = "application/javascript";
jsElement.src = "../constants.en.js";
document.body.appendChild(jsElement);
var script = document.createElement("script");
script.src = "../js/RandomScript.js";
script.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(script);
</script>
<!-- <script type="text/javascript" src="../js/RandomScript.js"></script> -->
</body>
I have got the following scenario and problem to solve. Any help will be appreciated.
Scenario
I have an angular application which calls a service(web api) and gets the contents back. The content is added to the html div. The content is as follow:
<script type="text/javascript" src="https://xxxx.com/ws/js/xxxxloader.js" id="theLoader"></script>
<script type="text/javascript">
// The code here look at the if the above script has been loaded properly by //checking the state of above script tag
</script>
I am adding the returned content to the HTML of the component in NgOnInit() as follow.
ngOnInit(){
this.apiService.getContent().subscribe(
d => {
this.responseString = d.toString();
var frag = document.createRange().createContextualFragment(this.responseString);
document.getElementById("theContainer").appendChild(frag);
}
);
}
However, it is failing because the first script tag (#theLoader) has never been loaded properly. I think it is because I am injecting the script tag after Dom has finished loading.
If you can see the better way of injecting the script, can you enlighten me.
Thanks in advance.
You can dynamically inject the JS URL by creating dynamic script tag
ngOnInit(){
function loadScripts(url: string){
var el = document.createElement('script');
el.onload = function(script){
console.log(script + ' loaded!');
// you can call the JS Function
};
el.src = url;
document.getElementsByTagName('body')[0].append(el);
}
</script>
ngAfterViewInit () {
this.loadScript('path to your js');
}
loadScript(url) {
console.log('preparing to load...')
let node = document.createElement('script');
node.src = url;
node.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(node);
}
Try this one :
ngOnInit(){
this.addScripts("https://xxxx.com/ws/js/xxxxloader.js", 'text/javascript');
this.addScripts("https://xxxx.com/js/mail.js", 'text/javascript');
}
addScripts(src, type): void{
var script = document.createElement('script');
script.src = src;
script.type = type;
document.querySelector('body').appendChild(script);
}
I've got this script
<script type="text/javascript">
var script = document.createElement('SCRIPT');
script.type = 'text/javascript';
script.src = 'http://streamunit.nl:9031/stats?json=1&callback=serverInfo';
document.body.appendChild(script);
</script>
var toto = "";
function serverInfo(res) {
var str = res.songtitle;
But I need to change the script.src to a xml and get the songtitle from that xml. Because my server doesn't have the json.
The xml file is the same as on this server http://streamunit.nl:9031/stats?sid=1
Who can help me change this script?
Fixed it.
Used a proxy to get te same json over https (SSL)
I would like to have the following on my page:
a script ("script1.js") that adds a second script ("script2.js") dynamically to the page.
a third script ("script3.js").
"script3" overwrites some of the functions in "script2", therefore the order that the scripts should be parsed is:
script1 > script2 > script3.
And the console should show: 1 2 3. Instead I see 1 3 2.
I have tried using "defer" on "script3" with no success:
HTML file:
<html>
<head>
<script src="script1.js"></script>
<script defer type='text/javascript' src="script3.js"></script>
</head>
</html>
script1.js:
console.log('1');
var head = document.getElementsByTagName('head')[0];
const url = 'script2.js';
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
head.appendChild(script);
script2.js:
console.log('2');
script3.js:
console.log('3');
As ugly as it sounds, you could use document.write inside script1.js to load script2.js. The scripts will execute in correct order.
// script1.js
console.log('1');
document.write('<script src="script2.js"></script>');
I am just try inject JW player into the page but I keep getting the error:
14 Failed to execute 'write' on 'Document': It isn't possible to write
into a document from an asynchronously-loaded external script unless
it is explicitly opened.
My code is as follows:
<html>
<body>
<div id="jw-live"></div>
</body>
<script type="text/javascript">
var url = new URL(window.location);
var id = url.searchParams.get("id");
var script = document.createElement("script");
script.src = "https://content.jwplatform.com/players/"+ id +".js";
script.type = 'text/javascript';
script.async = true;
window.document.getElementById("jw-live").appendChild(script);
</script>
</html>
Am I doing something wrong?