Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据JavaScript中的事件对象获取文件名?_Javascript_Jquery_Html - Fatal编程技术网

如何根据JavaScript中的事件对象获取文件名?

如何根据JavaScript中的事件对象获取文件名?,javascript,jquery,html,Javascript,Jquery,Html,假设一个HTML文件page1.HTML有一个脚本标记,用于base.js。在base.js(即外部JavaScript文件)中,我将onclick listener分配给page1.html中存在的按钮。此外,base.js包含在跨page1.html、page2.html等的script标记中,并且每个页面*?*.html中的每个按钮都分配了相同的onclick侦听器 现在,我需要知道在base.js中哪个HTML文件调用了onclick侦听器,换句话说,在哪个page*?*.HTML网页中

假设一个HTML文件
page1.HTML
有一个脚本标记,用于
base.js
。在
base.js
(即外部JavaScript文件)中,我将onclick listener分配给
page1.html
中存在的
按钮。此外,
base.js
包含在跨
page1.html
page2.html
等的
script
标记中,并且每个
页面*?*.html
中的每个
按钮都分配了相同的onclick侦听器

现在,我需要知道在
base.js
中哪个HTML文件调用了onclick侦听器,换句话说,在哪个
page*?*.HTML
网页中触发了该方法。理想情况下,我会得到
页面*?*.html
的文件名,在哪里?可能是1、2、3等等。有没有办法做到这一点?代码应该是这样的

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

谢谢大家!

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


在这个页面上,是“如何基于javascript中的事件对象获取文件名”

完美答案;我必须为这样一个显而易见的问题道歉。我认为它会更复杂,因为外部JS文件中的onlick事件。。。