1

I have a script named as view.js which does is counts the number of times a single div box is viewed on the page..... I have that mechanism well working when I load that script into only one div ...... but when I put that inside multiple divs inside single page it only works for the last script...... I know that js are loaded sequentially on a page ie only my last loaded scipt is suppose to work

can anyone tell me a way so that when I load my js multiple times inside different div they act as different scripts and run in parallel for count

and I also want to mention that the variable names will remain same for every script and I just want to use them differently for other scripts

also is there any solution using the js object... please let me know of the solution

<div id="parentofad" >
    <div id="ad">
        <img src="gemma-correll-cool-beans-button-magnet-lg.jpg" width="100" height="100">
    </div>
    <script type="text/javascript" src="adTagScript.js"></script>
</div>
<div id="parentofsecondad" >
    <div id="ad">
        <img src="gemma-correll-cool-beans-button-magnet-lg.jpg" width="100" height="100">
    </div>
    <script type="text/javascript" src="adTagScript.js"></script>
</div>

Edited: jsfiddle snippet https://jsfiddle.net/1fxgwztk/

1
  • Could you please post the code snippet, it may help? Commented Oct 10, 2016 at 7:54

1 Answer 1

2

Right now i guess you have a function that you call on page load for a specific class or id. What you must do is to initial that function for every class like this example

//Class
function Viewed(divID) { }

var div = new Viewed(divID);
Sign up to request clarification or add additional context in comments.

6 Comments

<div id="parentofad" > <div id="ad"> <img src="gemma-correll-cool-beans-button-magnet-lg.jpg" width="100" height="100"> </div> <script type="text/javascript" src="adTagScript.js"></script> </div> <div id="parentofsecondad" > <div id="ad"> <img src="gemma-correll-cool-beans-button-magnet-lg.jpg" width="100" height="100"> </div> <script type="text/javascript" src="adTagScript.js"></script>
this is the sample code for file and script is src="adTagScript.js" , now tell me how can I instantiate object for each time the script is loaded at the page
and I do not call any function I just load the script, so function has to be called on action on the scipt
create a jsfiddle or jsbin on your question for us to help you
here is the jsfiddle I have all the code but it may not work properly on jsfiddle because of the external files and links
|

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.