I have a javascript file that I reference in HTML with standard <script src="foo.js?param"></script>
. In the file I want to distinguish e.g. loading the file with foo.js
from foo.js?auto
and foo.js?noauto=true
, but not if the file is renamed to bar.js
and referenced with the same parameter. How can I accomplish this, preferably not using any framework?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Take a look a scriptaculous' approach:
I think it runs through all script elements and finds the reference to itself, and then parses that URL.
If you want to do it reliably, then you need to provide different JS for each URL. You could generate it with a server side script.
If you want to do it in pure JS, then you can
getElementsByTagName('script')
and assume that last script is the one that matched (it might not be, e.g. if script elements are being added using DOM after the page has loaded).