0

I want to include a local .js file in my index.html page, based on the outcome of an 'if' statement, as described in my sample code below. I need the .js contents to be included in the initial loading of the page, as the contents will be displayed in the html itself on load.

index.html:

<html>
  <head>
     <script src="script.js"></script>
  </head>
<body>
     <script>
         include_another_script();
     </script>
</body>
</html>

script.js:

include_another_script function(){
    var url;
    if( //some stuff){
        url = "script1.js";
    } else {
        url = "script2.js";
    }

    document.write("<script src=url></script>");
}
0

3 Answers 3

1

Try this instead

<head>
<script type="text/javascript">

var url;
if( blah == blah){
url = 'myUrl.js';
}else{
url = 'myOtherUrl.js';
}

var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = url;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);

</script>
</head>
Sign up to request clarification or add additional context in comments.

3 Comments

Ooops, I was sloppy when I wrote the sample code. Yes, I have done that in my working version. Still doesn't solve my problem :(
@p1xelarchitect in that case, can you show a more complete sample? i don't see any reason for it to not work.
And instead of using a function just write the if/else statement directly above this to get the var url value
0

If you need the script to be loaded before the document loads, you'll need to include the conditional script in the head section of the document.

You can do something like this:

<head>
    <script src="script.js"></script>
    <script type="text/javascript">
        function include_another_script(){
            var url;
            if( //some stuff){
                url = "script1.js";
            } else {
                url = "script2.js";
            }
            $('head').append('<script type="text/javascript" src="'+url+'"></script>');
        }
   </script>
</head>

Note that your function declaration is wrong:

include_another_script function() {

6 Comments

Why does it need to be in the <head>?
@KevinB He said "I need the .js contents to be included in the initial loading of the page, as the contents will be displayed in the html itself on load". Putting them in the head ensures that they will.
He is putting it in the head since the OP asked to have it load before anything else in the document
that doesn't necessarily mean it needs to be in the head. It all depends on what the code does. If the code he's including does a document.write, he'll want to include it using document.write, where he intends for it to output.
@KevinB Ok man, you win...we'll just delete our answer and you can provide him the solution.
|
0

Try this, src is the path to the script you want to load.
var script = document.createElement('script');
script.src = src;
document.head.appendChild(script);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.