[英]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
. 想象一下,HTML文件
page1.html
有一个base.js
的脚本标记。 In base.js
(ie external JavaScript file), I assign onclick listener to a button
that exists in page1.html
. 在
base.js
(即外部JavaScript文件)中,我将onclick监听器分配给page1.html
中存在的button
。 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. 此外,
base.js
包含在page1.html
, page2.html
等的script
标记中,每个page*?*.html
每个button
page*?*.html
都分配了相同的onclick监听器。
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. 现在,我需要以某种方式在
base.js
中知道哪个HTML文件调用了onclick监听器,换句话说,在哪个page*?*.html
网页中触发了该方法。 Ideally, I would get the filename of that page*?*.html
, where ? 理想情况下,我会得到该
page*?*.html
的文件名page*?*.html
,在哪里? could be 1, 2, 3, etc. Is there a way to accomplish that? 可能是1,2,3等。有没有办法实现这一目标? 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! 谢谢!
var filename = location.href.match(/([^\\/]*?)$/)[1];
在这个页面上,它是“如何获取基于文件名的事件对象在javascript中”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.