2012-01-03 42 views
1

私は持っていると言っていますhttp://www.mysite.com/index.php?=332jQueryを使用してURLから文字列を取得しますか?

?=の後ろの文字列をjQueryで取り出すことは可能ですか?私はGoogleの周りを見てきただけで、AjaxとURL varsの情報がたくさんあるので、私には分かりません。

if (url.indexOf("?=") > 0) { 
    alert('do this'); 
} 
+4

まずGoogleの結果の名前を提供することで、クエリ文字列から値を返すために(私が、ソースを覚えていないではない)のコードスニペットである - のhttp: //jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html – FlabbyRabbit

+0

これは有効ですか? '?= 332' *私はそれが... * –

+1

の可能な複製[javascriptでクエリ文字列を取得する方法は?](http://stackoverflow.com/questions/2907482/how-to-get-the-クエリ文字列ごとにjavascript) –

答えて

1

window.location

あなたの友達です。具体的window.location.search

0

まず、あなたのクエリ文字列が正しくない場合、あなたは単にindexOfの間の部分文字列を取ることができます「?=」+ 1との長さ文字列。参照してください:http://www.w3schools.com/jsref/jsref_substring.asp

JQueryを使用しないと簡単にできるのはjsだけです。

0
var myArgs = window.location.search.slice(1) 
var args = myArgs.split("&") // splits on the & if that's what you need 
var params = {} 
var temp = [] 
for (var i = 0; i < args.length; i++) { 
    temp = args[i].split("=") 
    params[temp[0]] = temp[1] 
} 

// var url = "http://abc.com?a=b&c=d" 
// params now might look like this: 
// { 
//  a: "a", 
//  c: "d" 
// } 

何をしようとしていますか? URLを読んでいると間違っているかもしれません。ここ

+0

'window.location.href'は' window.location.search'にする必要があります – epascarello

+0

ああクールなもの!ありがとう –

0

$.urlParam = function(name){ 
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);   
if (!results) 
     { return 0; }   
return results[1] || 0; 

}

関連する問題