how to allow access to url only using a script on my server?

I have an url mydomain.com/myurl. I have a script on each page that calls this url after page is loaded and certain conditions are met. I would like to restrict access to this url only if it is called from my domain mydomain.com. How to achieve this? Maybe with a rule in htaccess? Thank you