Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. Here is how I would do it:
  2.  
  3. I would use classes instead of IDs, this way I don't have to care about unicity:
  4.  
  5. <script src="./bundle/src/js/api.js" class="test-api" data-client="2"></script>
  6. <script src="./bundle/src/js/api.js" class="test-api" data-client="5"></script>
  7. <script src="./bundle/src/js/api.js" class="test-api" data-client="6"></script>
  8.  
  9. Then, the first thing I would do inside the script is try to find `.test-api` scripts which have not been processed yet. And mark them with another class when I find them:
  10.  
  11. var elementsToProcess = document.querySelectorAll('.test-api:not(.processed)');
  12. for (var i = 0; i < elementsToProcess.length; i++) {
  13. var el = elementsToProcess[i];
  14. el.classList.add('processed');
  15. var clientId = el.getAttribute('data-client');
  16. initClient(clientId);
  17. }
  18.  
  19. function initClient(clientId) { /* Your logic for a given client... */ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement