Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22780

Re: Javascript conflicts

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Javascript conflicts
Date 2014-01-24 23:09 +0000
Organization A noiseless patient Spider
Message-ID <lburrt$rmi$1@dont-email.me> (permalink)
References <deee7c09-9d64-4b16-a668-bdb8dc747c80@googlegroups.com>

Show all headers | View raw


On Fri, 24 Jan 2014 14:58:33 -0800, WanWan wrote:

> How do I solve this?
> My script is intended to be inserted like this:
> 
> <script type="text/javascript" id="id1">var myId="id1";</script>
> <script type="text/javascript" src="myScript.js"></script>
> 
> then another place on the page:
> 
> <script type="text/javascript" id="id2">var myId="id2";</script>
> <script type="text/javascript" src="myScript.js"></script>
> 
> and so on.
> Where and how many times I cannot control. And where the script is,
> indicates where the "action" should happen.
> 
> The problem here is that the the "unique identifier" (id1, id2 ...) for
> each script written to the same variable, so it gets the value that is
> set at last before the script runs several times.

At each point in the page, instead of including the whole script, do 
something like:

<script type="text/javascript" id="some_id">
my_main_func("some_id");
</script>

Then wrap the code in myScript.js in a function[1]:

my_main_func( my_Id ) {

// original code goes here

}

and put the main script in the head of the html document once with:

<script type="text/javascript" src="myScript.js"></script>

Now, at each point in the doc where you were loading the whole script, 
instead you call the single copy of the script with the appropriate id 
value.

[1] If code.js includes definitions for global items (functions, objects, 
variables, classes etc) you may need to consider carefully whether you 
need to refactor some or all of those definitions outside of your main 
function wrapper.

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Javascript conflicts WanWan <jippijappo@hotmail.com> - 2014-01-24 14:58 -0800
  Re: Javascript conflicts "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-25 00:03 +0100
  Re: Javascript conflicts Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-24 23:09 +0000
    Re: Javascript conflicts WanWan <jippijappo@hotmail.com> - 2014-01-25 13:40 -0800

csiph-web