PacktPublishing / Django-3-by-Example

Django 3 by Example (3rd Edition) published by Packt
https://djangobyexample.com/
MIT License
716 stars 687 forks source link

Chapter 5 bookmarklet_launcher.js #101

Open data-scientist-ml1 opened 3 years ago

data-scientist-ml1 commented 3 years ago

Can anyone please help me understand its code?

(function(){ if (window.myBookmarklet !== undefined){ myBookmarklet(); } else { document.body.appendChild(document.createElement('script')).src='https://127.0.0.1:8000/static/js/bookmarklet.js?r='+Math.floor(Math.random()*99999999999999999999); } })();

  1. Why we need to put function inside parenthesis? (function.....)()
  2. How browser executes the code when we put javascript tag? JavaScript:(function.....)()
  3. what is this function myBookmarklet() and when if statement will be actually executed? How will window object will have myBookmarklet property?

Please share any resources as well. Thanks a lot