1

I noticed that you can't import a javascript file and define a variable in the same <script>.

This is my problem:

<script type="text/javascript" src="js.js">
var id = "587587";
</script>

Is there a way to load the javascript after the variable without using another <script> tag?

6
  • 4
    Why not simply use another script tag...?! Commented Jul 5, 2014 at 15:59
  • You could first define the variable and then do something like document.write('<script src="js.js"><\/script>') in the javascript. But then again, why not insert the script tag manually? Commented Jul 5, 2014 at 16:01
  • Because I'll be implementing it into a system where users will copy the code into their own websites, so it needs to be clean. Commented Jul 5, 2014 at 16:01
  • Writing a script tag programmatically is typically what most analytics scripts do. But some also use a second script tag. IMO it's not any worse, the user needs to copy and paste some meaningless blob anyway, he'll hardly care what exactly that blob contains. Commented Jul 5, 2014 at 16:04
  • 2
    @George - perhaps you could ask the (maybe a different) question in a way that illustrates the problem you're trying to solve instead of a work around for not being able to use the script tag like you want. You might get better answers. Commented Jul 5, 2014 at 16:04

3 Answers 3

1

If a script tag has a src attribute, anything between the script tags is ignored. The only way you could do something like this is by dynamically adding a new script tag with an ugly hack like this.

<script type="text/javascript">
var id = "587587";
document.write('<script type="text/javascript" src="js.js"><\/script>');
</script>

Using another script tag before the one with the id would certainly be the preferable and shorter solution.

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

Comments

1

src

This attribute specifies the URI of an external script; this can be used as an alternative to embedding a script directly within a document. script elements with an src attribute specified should not have a script embedded within its tags.

http://developer.mozilla.org/en-US/docs/Web/HTML/Element/script.html

So, you either have an external script, or you have something inside the <script> tag. You shouldn't have both at once; I'm not sure whether this actually does anything or not.

Use a second script tag.

2 Comments

the contents of a .scr-having tag don't "do anything", but they are present in the dom by any other script.
@dandavis "...whether having source code within the tags when you already have a src attribute does anything..."
0

A script element must either contain script code between the script start and end tags, or reference external script code via the src= attribute. It cannot do both. However, your external code can reference the details of your HTML via the DOM (Document Object Model). So you can treat any HTML element (e.g. a text input field) as a parameter to be passed to your external javascript, where it can be read by e.g. var param = document.getElementById('param').innerHTML;

Comments

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.