2016-05-13 50 views
0

我正嘗試讀取JavaScript文件中的查詢參數。從文件路徑中讀取查詢參數

代碼 -

的index.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>Chat App List</title> 
</head> 
<body> 
    <div id="app"></div> 
    <script src="bundle.js?cid=vivek"></script> // I am trying to read value of cid in bundle.js file 
</body> 
</html> 

到目前爲止,我試圖在包文件中使用window.location.href但它給undefined,因爲它從URL讀取和網址提供index.html文件

+0

哪個查詢參數? – d4rty

+1

檢查此問題http://stackoverflow.com/questions/4716612/how-do-i-get-query-string-value-from-script-path – GBnewbie

+0

嘗試獲取腳本標記的引用並訪問其src 'property – mr5

回答

0

windows.location.href會給你你在瀏覽器的地址欄中看到的地址。

雖然你可能會解析DOM並找出你需要的東西,但是肯定沒有建議你在代碼中得到這個查詢參數的方法。

編輯:發現同一問題較早問 - Get the query string on the called javascript file

+0

window.localtion.href將不起作用,因爲文件是來自index.html的服務器。我從DOM讀過它 – NeiL

相關問題