2

Yo!

I have an arbitrary javascript file, let's call it localScript, and just say it looks something like this:

<script id="myScript" type="text/javascript">
    function () {
        var blue = 'blue';
        var person = {
           firstName:"John",
           lastName:"Doe",
           age:50,
           eyeColor:"brown"
        };
        var bluePerson = function () {
           person[color] = blue;
        };
    }
</script>

I want to be able to use another externalScript to dynamically change the contents of this localScript. For this simple example, let's just say I want to update some of the values in localScript, like—maybe change age of the person object to 75. (Obviously, there's very simple ways to do this, but for my use case it's imperative that I use another externalScript to generate the contents of this localScript).

It would be easy if there was something like .innerHtml which I could use in the externalScript which would allow me to select an element and then replace the 'innerHtml' contents. The localScript, though, obviously isn't composed of elements.

As far as I know, when using a script to modify another script, there aren't any 'easy' ways to reference variables/objects/items in the script.

Things I've considered are indexOf(), search(), and match(), which I could use in externalScript to find strings inside localScript and then replace the values. I feel though as these could be performance no-no's, especially if the script grows.

Are there any easy ways to do this—with an emphasis on performance? I feel like there must be some easy way to reference one of the items in the script, though, I suppose a script is all one large string.. and maybe there is no simple way.

BTW—I'm using AngularJS, if there are any built in methods—though I think this is mostly just a javascript thing.

Thanks a bunch!

5
  • 1
    All of what you have shown is in the global window space. So it can be accessed from anywhere. So therefore is hard to see where you have a problem unless you have oversimplified the example Commented May 14, 2015 at 2:04
  • 1
    OK..that change made it checkmate Commented May 14, 2015 at 2:06
  • I feel like we might need more context on this problem. What is preventing you from passing in a person object? or passing in the 'Blue' variable? Are you not allowed to modify the external script? Commented May 14, 2015 at 2:12
  • In this specific context, the DOM loads, the localScript executes and then I prefer to change some values and then have it re-execute, via eval(). Commented May 14, 2015 at 2:14
  • WHat is the actual use case of existing script and what are you trying to overcome? Commented May 14, 2015 at 2:15

2 Answers 2

1

It looks like a bad idea, but... well, if it is imperative...

It makes no sense to change a script in a <script> tag - if it is in DOM, it has already executed (and no longer matters). Thus, to change the script before it has a chance to execute, you need to load it using AJAX, change the text, then eval it.

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

1 Comment

Agreed that 99% of the time it's definitely an odd thing to do. I think there's a weird occasion here or there where it could be useful. I am re eval()'ing it via the other script already so that it re-executes. Thanks though :-)
0

You can easily change the variables. Refer following steps

  1. Include external script just below the script you have written.
  2. Access the variables in the external script as if they are locally declared.

The variables you have created in above script are available in global scope and hence should be accessible from everywhere.

Note: This answer was added before the function clause was added.

1 Comment

Apologies. Forgot to mention a slightly complicating detail that they are local variables :-)

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.