2012-04-03 71 views
2

我知道這個問題之前已經被問過了,但是到目前爲止我發佈的所有解決方案都遇到了問題。我有一個像這樣的網址:使用jQuery/Javascript訪問GET變量

http://localhost:3000/virgil/1/render_lesson?toggle=view 

這是一個Rails通過AJAX調用,但這應該不重要。每次我嘗試一個應該得到「切換」變量的函數時,我將其打印到控制檯日誌中時會得到「未定義」。我曾嘗試這個功能

function getUrlVars() 
{ 
    var vars = [], hash; 
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
    for(var i = 0; i < hashes.length; i++) 
    { 
     hash = hashes[i].split('='); 
     vars.push(hash[0]); 
     vars[hash[0]] = hash[1]; 
    } 
    return vars; 
} 

當我打電話console.log(getUrlVars());我得到["http://localhost:3000/"]

但是,當我打電話console.log(getUrlVars()["toggle"]);我只是得到undefined

我明白任何幫助!

編輯:

添加我的代碼的相關部分。這是一個Rails應用程序,它使用AJAX進行調用。

<%= link_to 'View Lessons', render_lesson_virgil_path(course, :toggle => :view), :remote => true, :class => 'toggle_lesson', :id => "toggle_lesson#{course.id}" %>

然後,它調用javascript文件:

function $_GET(name){ 
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
    var regexS = "[\\?&]"+name+"=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(window.location.href); 
    if(results == null) 
    return ""; 
    else 
    return results[1]; 
} 

console.log($_GET('toggle')); //This is where it prints the incorrect value to the log, regardless of the function I use. 

$("#Course<%= @course.id %>").append('<div id="Lesson<%= @course.id %>" class="render_lesson"></div>'); 
$("#Lesson<%= @course.id %>").html("<%= escape_javascript(render(:partial => "lesson")).html_safe %>"); 
+0

可能重複[如何獲得與jQuery GET和POST變量?](http://stackoverflow.com/questions/439463/how-to-get-get - 和後變量與jQuery) – Bozho 2012-04-03 21:34:06

+0

和http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery – Bozho 2012-04-03 21:34:22

+0

我改變你的函數接受一個URL字符串,而不是從窗口抓取它。位置。我把你的URL作爲一個字符串推入,它對我有用;我得到了一個具有「切換」鍵和「查看」值的數組。 – SuperJumbo 2012-04-03 22:14:12

回答

3

這是JS功能,我使用的查詢字符串 - 希望它可以幫助你。

function getParameterByName(name) { 
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search); 
return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); 
} 
+0

這似乎有相同的問題。當我調用'console.log(getParameterByName('toggle'));'我得到'null'而不是正確的值。 – Rhawb 2012-04-03 21:52:34

+0

它適合我。 – 2013-07-06 14:31:23

0

試試這個(我只改變的線4 - 「VAR哈希...」):

function getUrlVars() 
{ 
    var vars = [], hash;  
    var hashes = window.location.href.valueOf().split('?').slice(1)[0].split('&'); 
    for(var i = 0; i < hashes.length; i++) 
    { 
     hash = hashes[i].split('='); 
     vars.push(hash[0]); 
     vars[hash[0]] = hash[1]; 
    } 
    return vars; 
} 
-2

我使用它在我的項目1項..

function $_GET(name){ 
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
    var regexS = "[\\?&]"+name+"=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(window.location.href); 
    if(results == null) 
    return ""; 
    else 
    return results[1]; 
} 

看起來像PHP $_GET[''] ...因爲我愛PHP:D

用法示例:

網址:index.php?page=newone&id=4&cat=15

的Javascript:alert($_GET('page')); // newone

+0

奇怪的是,當我打印到console.log時,我得到了'(一個空字符串)' – Rhawb 2012-04-03 23:48:06

+0

@Rhawb:我現在再次測試它..它正在爲我工​​作..你能告訴我你的代碼..你是使用... – Shahrukh 2012-04-03 23:57:48

+0

我已經發布我的代碼在原始發佈。謝謝! – Rhawb 2012-04-03 23:59:49