i want to add different javascript to different divs. Is it posible to set .js page's scope to specific


div> ?

Recommended Answers

All 4 Replies

No you cannot "set .js page's scope to specific <div>".

In client-side javascript the global scope is always window. This cannot be changed.

In addition to the global scope, other scopes may also exist at runtime, as determined by the structures within the code that you have written, particularly functions.

Javascript can be, and more often than not is, written to address specific DOM elements, including divs, typically to read/write their contents and/or change their appearance.

JavaScript objects/variables can also be arbitrarily assigned to DOM elements. This is a "namespacing" technique and not "scope" in the traditional computer lanuage sense. This last point has caused me to think hard and I would be pleased to know other people's opinion.


The terminology used is not exact, but we could call it a contextual script element, or something.
Such thing was possible in Explorer.

<div id=div1> div1 content </div>
<script for=div1 event=onlouseover src=div1.js></script>

But it becomes expensive, I never used it..

adn I liked the old editor better!

ofcourse you can use a diferent eventhander on every div
<div id="div1" onmouseover="handeldiv1()" >

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.