2015-09-25 20 views
-2

我要動態地從行書位置如何檢測當前的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。

+0

'window.location' – hjpotter92

+0

頁面URL是什麼樣的? – lshettyl

+0

@ hjpotter92我不能使用windows.location,因爲使用mvc和位置diffrent by filename –

回答

0

這裏是你如何能得到jQuery的當前文件名:

$(location).attr('pathname'); // the filename (ie: index.php) 

如果你想完整的URL做到這一點:

$(location).attr('href');  // the full url 

如果你把文件名中,你可以做一個變量你想用它什麼...

下面是一個例子小提琴:http://jsfiddle.net/jj0zb9uu/ 希望幫助你...

+0

我不能使用windows.location,因爲使用mvc和位置不同的文件名 –

相關問題