1

Imagine that an HTML file page1.html has a script tag for base.js. In base.js (i.e. external JavaScript file), I assign onclick listener to a button that exists in page1.html. Furthermore, base.js is included in script tags across page1.html, page2.html, etc., and each button in each page*?*.html has the same onclick listener assigned to it.

Now, I need to somehow know in the base.js which HTML file invoked the onclick listener, in other words, in which page*?*.html webpage the method was fired. Ideally, I would get the filename of that page*?*.html, where ? could be 1, 2, 3, etc. Is there a way to accomplish that? The code should look like this...

// fires up when a button in 'page*?*.html' is clicked function myOnClickEventHandler(e) { // the file where myOnClickEventHandler originated from is: var filename = e.??????? } 

Thank you!

1 Answer 1

3

var filename = location.href.match(/([^\/]*?)$/)[1];

on this page, it's "how-to-get-filename-based-on-the-event-object-in-javascript"

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer; I must apologize for such an obvious question. I thought it would be more involved because of the onlick event in the external JS file...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.