Error In Embed Script Into Page: 'Failed to execute 'write' ' - javascript

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?

Related

Loading sequence of scripts in html

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>

Need to change json to xml content in 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)

JS: parse dynamically added script before other scripts

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>');

JavaScript URL checking for pupunder

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>

Error when trying append script

I trying to append scripts in client side:
example :
var script = '<script type="text/javascript">document.write(\'<script type="text/javascript" src=""><\\/script>\');</script> '
$('body').html(script );
getting error : Uncaught TypeError: Cannot call method 'hasAttribute' of null
Try this:
var scriptlog =document.createElement('script');
scriptlog.type ='text/javascript';
scriptlog.src =url;
$('body').append( script );
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src",
"your js url");
By JAVASCRIPT
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("script")[0] || document.documentElement).insertBefore(script_tag);
By JQUERY
$("head").append(script_tag);
If you want to Dynamically add Scripts inside HTML element -
var script = document.createElement( "script" );
script.type = "text/javascript";
script.src = "script.js";
$("body").append(script);
Refrence
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'a.js';
document.head.appendChild(script)

Categories