Wray Bowling
Wray Bowling

Reputation: 2506

pass objects into external js with script tag

Just out of sheer curiosity, I'd like to know if there's a technique for passing things (most likely a javascript object) to external javascript within the script tag that loads that script.

<script type="text/javascript" src="example.js">
{example:'something like this'}
</script>

I'm almost certain I've seen this done somewhere once before. I find it very tidy and would like to be able to do it for libraries i'm writing so users can pass in simple options without needing to make another script.

Upvotes: 1

Views: 1570

Answers (1)

frosty11x
frosty11x

Reputation: 205

As with what the Question Ballbin has posted says, "It should be avoided.".

But, a different way you can do it is with another script tag.

Like this:

<script>var test1 = "Testing";</script>
<script type="text/javascript" src="example.js"></script>

The example.js file should now be able to read the contents of test1.

Upvotes: 1

Related Questions