Alex Alex - 7 months ago 17
Javascript Question

How to get filename based on the event object in JavaScript?

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!

Answer

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

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