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


Groups > comp.lang.javascript > #22778 > unrolled thread

Javascript conflicts

Started byWanWan <jippijappo@hotmail.com>
First post2014-01-24 14:58 -0800
Last post2014-01-25 13:40 -0800
Articles 4 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#22778 — Javascript conflicts

FromWanWan <jippijappo@hotmail.com>
Date2014-01-24 14:58 -0800
SubjectJavascript conflicts
Message-ID<deee7c09-9d64-4b16-a668-bdb8dc747c80@googlegroups.com>
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.

[toc] | [next] | [standalone]


#22779

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-01-25 00:03 +0100
Message-ID<XnsA2C0A13396Beejj99@194.109.133.133>
In reply to#22778
WanWan <jippijappo@hotmail.com> wrote on 24 jan 2014 in
comp.lang.javascript: 

> 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. 

Why is that a problem?

Why would you need to id the <script>?

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#22780

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-01-24 23:09 +0000
Message-ID<lburrt$rmi$1@dont-email.me>
In reply to#22778
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

[toc] | [prev] | [next] | [standalone]


#22788

FromWanWan <jippijappo@hotmail.com>
Date2014-01-25 13:40 -0800
Message-ID<c2b4a40a-4ee6-4eb3-b5a7-204732a3484a@googlegroups.com>
In reply to#22780
Thank you, excellent answer.

kl. 00:09:49 UTC+1 lørdag 25. januar 2014 skrev Denis McMahon følgende:
> 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.
> 
> 
> 
> -- 
> 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web