2011-11-04 3 views
2

PHP와 비슷한 URL 내에서 POST를 위해 jQuery로 확인할 수있는 방법이 있습니까?jQuery - URL에 POST가 있는지 확인하십시오.

PHP 버전 :

if(!empty($_GET)) { // do stuff } 

죄송합니다. 내 말은 GET은 POST가 아닙니다. 내가 페이지로드 $(document).ready(function() { // here });

예 URL의 URL을 가져 오기 위해 찾고 있어요

: 함수 OnLoad();

+0

당신은 $ _POST가 URL에 전달되지 않는다는 것을 알고 있습니다. 권리? – MarioRicalde

+0

javascript/jQuery에서 URL의 매개 변수를 가져 오시겠습니까? – ManseUK

+0

예. 죄송합니다. POST하지 마라. – Iladarsda

답변

3

URL을 얻기 위해 jQuery를 확장은

다음
$.extend({ 
    getUrlVars: function(){ 
    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; 
    }, 
    getUrlVar: function(name){ 
    return $.getUrlVars()[name]; 
    } 
}); 

당신은 당신이 얻을

if($.getUrlVar('name') == 'frank'){ 
+0

예제가 훌륭하게 작동합니다. ''OnLoad' 함수에'.getUrlVar ('name')'값을 전달하는 것은 어떻습니까? – Iladarsda

+0

줄을 추가하면 $ (document) .ready (function() { methodForDoingCoolStuffForFrank ($. getUrlVar ('name'));/* onload에서 호출 할 메서드 */ }); 그러면 페이지가 준비 될 때 호출되어 이름 값을 기반으로 작업을 수행 할 수 있습니다. – kamui

1

번호 POST 데이터로 http://www.domain.com?a=1

을 그리고 value of the var a 과거보다 자바 스크립트에 액세스 할 수 없습니다 .

PHP에서 체크를하고 결과에 대해 알 수 있도록 일부 JavaScript 설정 변수를 출력해야합니다.

if(!empty($_POST)) 
    echo "<script type='text/javascript'> post_not_empty = true; </script>"; 
+0

'http : //www.domain.com? a = 1 & b = 2' URL에서 데이터를 가져 오는 것은 어떻습니까? – Iladarsda

2

새 응답

그냥 경우 쿼리 문자열이 여부를 확인하기 위해 자바 스크립트를 사용합니다. 쿼리 문자열 내에서 실제 데이터를 얻을 필요가있는 경우

if(location.search.length > 0) 
    alert('Horay horah!'); 

당신은 Query String Object jQuery 플러그인을 사용할 수 있습니다, 또는 당신은 location.search 텍스트를 구문 분석에 대한 이동합니다.

2

JQuery를 사용하여 QueryString 매개 변수를 가져 오려면 다음 코드를 사용하는 것이 좋습니다. 전역 변수에 매개 변수를 저장하는 것이 좋습니다 (불만을 제기하십시오!

alert(urlParams["abc"]);

http://www.mywebsite.com/open?abc=123&def=234의 값이 "123"로 경고를 표시 =

URL; 사용량

var urlParams = {}; 
(function() { 
    var e, 
     a = /\+/g, // Regex for replacing addition symbol with a space 
     r = /([^&=]+)=?([^&]*)/g, 
     d = function (s) { return decodeURIComponent(s.replace(a, " ")); }, 
     q = window.location.search.substring(1); 

    while (e = r.exec(q)) 
     urlParams[d(e[1])] = d(e[2]); 
})(); 

예 :)와 그 변수에 쿼리 스트링을 분석

+0

복잡해 보입니다. urlParams를 배열로 반환합니까? 내 경우에는 하나의 매개 변수 만있을 것입니다. – Iladarsda

+0

그것의 복잡하지 않은 - 그것의 querystring의 분할 - 나는 당신에게 사용 예제를 제공하는 답변을 업데이 트했습니다. – ManseUK

+0

사용법은 간단합니다. 그러나 자신을 가져 오는 방법은 매우 복잡해 보입니다 - 내 급료는 아닙니다! :) – Iladarsda

1

하여 jQuery 플러그인을 특정 값을 조회 할 수 있습니다

if($.getUrlVars().length > 0){ // do something } 

를 호출 할 수 있습니다 바르 완료 :

/* 
jQuery Url Plugin 
* Version 1.0 
* 2009-03-22 19:30:05 
* URL: http://ajaxcssblog.com/jquery/url-read-get-variables/ 
* Description: jQuery Url Plugin gives the ability to read GET parameters from the actual URL 
* Author: Matthias Jäggli 
* Copyright: Copyright (c) 2009 Matthias Jäggli 
* Licence: dual, MIT/GPLv2 
*/ 
(function ($) { 
    $.url = {}; 
    $.extend($.url, { 
     _params: {}, 
     init: function() { 
      var paramsRaw = ""; 
      try { 
       paramsRaw = 
        (document.location.href.split("?", 2)[1] || "").split("#")[0].split("&") || []; 
       for (var i = 0; i < paramsRaw.length; i++) { 
        var single = paramsRaw[i].split("="); 
        if (single[0]) 
         this._params[single[0]] = unescape(single[1]); 
       } 
      } 
      catch (e) { 
       alert(e); 
      } 
     }, 
     param: function (name) { 
      return this._params[name] || ""; 
     }, 
     paramAll: function() { 
      return this._params; 
     } 
    }); 
    $.url.init(); 
})(jQuery); 

사용 예 : $.param('a')

관련 문제