如何检测当前的Java脚本代码的文件名乳宁
问题描述:
我要动态地从行书位置如何检测当前的Java脚本代码的文件名乳宁
例如发送当前的文件名:
的index.php
<html>
<head>
<script src="script.js"></scritp>
</head>
<body>
...
</body>
</html>
的script.js
$(function(){
$data = [];
$.getJSON("data.php",{ajax_sending_filename : FILE_NAME},
function(response){
$data = jQuery.parseJSON(response);
});
});
我需要动态获取当前文件名并将其替换为FILE_NAME:
$.getJSON("data.php",{ajax_sending_filename : 'index' //FILE_NAME},
请提出一种适用于所有浏览器的方法。
感谢您的帮助:-)
编辑:
,因为使用MVC和地点,按文件名不同,我cant't使用windows.location。
答
这里是你如何能得到jQuery的当前文件名:
$(location).attr('pathname'); // the filename (ie: index.php)
如果你想完整的URL做到这一点:
$(location).attr('href'); // the full url
如果你把文件名中,你可以做一个变量你想用它什么...
下面是一个例子小提琴:http://jsfiddle.net/jj0zb9uu/ 希望帮助你...
+0
我不能使用windows.location,因为使用mvc和位置不同的文件名 –
'window.location' – hjpotter92
页面URL是什么样的? – lshettyl
@ hjpotter92我不能使用windows.location,因为使用mvc和位置diffrent by filename –