1

I dont know if this is a good idea but I am trying to add a script using document.write in my HTML document that would add a script only if the browser is online.

<script>
var online = navigator.onLine;
if(online){
  document.write("<script src='./api.js'></script>");
}
</script>

the thing is the browser is stoping at the first closing </script> tag that is inside the document.write function, causing everything else to be interpreted as HTML ( "); appears renedered in the html) , I know that I am doing something wrong but would like to know what

3
  • Note: Why split the <script> tag when writing it with document.write()? Commented Jul 8, 2014 at 21:32
  • Note the return value of navigator.onLine wither true or false will not definitively let you know wither the user has an active internet connection or not. Commented Jul 8, 2014 at 21:32
  • this is not an app for the general public , it's an isolated app that i will be showcasing on a screen using chrome at a gallery and therefore external users and browser fallbacks have been disregarded Commented Jul 8, 2014 at 21:39

3 Answers 3

2

Try this:

document.write("<script src='./api'></"+"script>");

You're right. Browsers read all <script> ... </script> block and pass them to JS engine. So, if you want to write </script> you should break it.

Note Using document.write to add JavaScript to document is not a good idea.

Sign up to request clarification or add additional context in comments.

1 Comment

It works. Thanks. It may not be a good idea, but for quick fix for footer page includes, worked great.
1

EDIT: please see this thread and answers: https://stackoverflow.com/a/8024286/2192152

Here is a code I used for a personal website:

/* *************************************************************
 *  Loading any js file.
 ************************************************************** */
staticPath ='./js/';
vendorPath='./js/vendor/';

src = [
    {f:'jquery-1.8.3.min.js', n:1},
    {f:'jquery-ui-1.9.2.js', n:1},
    {f:'lib.js', n:0},        
];


for (var i in src)
{
    if (src[i].n === 0) document.write('<' + 'script type="text/javascript" src="' + staticPath + src[i].f + '"></sc' + 'ript>');
    else  document.write('<' + 'script type="text/javascript" src="' + vendorPath + src[i].f + '"></sc' + 'ript>');
}

As you can see you should split the string containing you script call. I unfortunately don't know much more of why this is needed.

Note: I tried many setting and the one I provide you here is the fastest one in terms of load time (using chrome)

1 Comment

1

You could try:

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "./api.js";
document.head.appendChild(script);

also are you missing extension on api? should it be api.js?

2 Comments

it somehow works without the extension as a js file but adding it would be more intuitive, i will edit my question for the sake of better understanding
just change api in the line script.src = "./api"; to correct filename after you change it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.