2012-03-28 24 views
0

我有可能是一個網址www.website.com/order/?pg3=2www.website.com/orderstatus/?pg2=如果url變量爲空,如何檢查JavaScript?

有可能在年底幾種組合和數量,所有我想真的是檢查PG2或PG3是空的?

我不知道如何實際訪問url中的post變量,有時url可能沒有變量在所有tho?

$(document).ready(function() { 

    if(pg2 != ''){ 
    //Does a click 
    $("#add2").click(); 
    } 
    if(p3 != ''){ 
    $("#add3").click(); 
    } 

}); 

如何檢查頁面加載時pg2是否爲javascript中的空白?

+0

的[在JavaScript中獲取查詢字符串值]可能重複(http://stackoverflow.com/questions/901115/get -query-string-values-in-javascript) – 2012-03-28 10:55:25

回答

0

您可以分析與普通的舊的js查詢字符串,然後檢查是否特別PARAM填充 下面的功能會給你一個參數數組

var queryString = window.location.href.replace(/^[^\?]+\??/,''); 

var params = parseQuery(queryString); 
function parseQuery (query) { 
     var Params = new Object(); 
     if (! query) return Params; // return empty object 
     var Pairs = query.split(/[;&]/); 
     for (var i = 0; i < Pairs.length; i++) { 
      var KeyVal = Pairs[i].split('='); 
      if (! KeyVal || KeyVal.length != 2) continue; 
      var key = unescape(KeyVal[0]); 
      var val = unescape(KeyVal[1]); 
      val = val.replace(/\+/g, ' '); 
      if (! Params[key]) Params[key] = new Array(); 
      Params[key].push(val); 
     } 
     return Params; 
    } 
+0

'unescape'不**解碼查詢字符串參數。 – 2012-03-28 11:02:42

0

林不知道如何實際訪問中職變量網址

首先,如果他們在URL,他們GET變量,而不是POST變量(這是在請求正文中發送給服務器)。具體而言,它們是查詢字符串上的值。

您可以通過location.search訪問查詢字符串。不幸的是,它出現在地址欄中,而不是很好地解析出來,但有一些插件可以用來解析它,like this one可以讓你訪問你的pg2這個變量:

var pg2 = $.url(location).param('pg2'); 

pg2變量將是undefined如果有查詢字符串沒有匹配的參數。

這只是一個例子,有幾個查詢字符串/ URL解析插件可用,或者當然你可以推出自己的。這裏有一個我做了幾年前:

/** 
* Split up the given string (for instance, window.location.search) into component parts. 
* 
* @param str   The string 
* @param The component parts as keys on an object; if the query string has repeated entries, 
*   that key's value will be an array; a key with no value is present with the value 
*   `undefined`. 
*/ 
$.splitQueryString = splitQueryString; 
function splitQueryString(str) { 
    var entries, parts, result, entryIndex, key, newVal, oldVal; 

    // We return the result as an object 
    result = {}; 

    // Skip a leading ? if any 
    if (str.charAt(0) === '?') { 
     str = str.substring(1); 
    } 

    // Strip anything after the hash symbol 
    index = str.indexOf('#'); 
    if (index >= 0) { 
     str = str.substring(0, index); 
    } 

    // decodeURIComponent won't do '+' => ' ', so do it 
    str = str.replace(/\+/g, ' '); 

    // Split into entries 
    entries = str.split('&'); 
    for (index = 0; index < entries.length; ++index) { 
     parts = entries[index].split('='); 
     key = decodeURIComponent(parts[0]); 
     newVal = parts[1]; 
     if (typeof newVal !== 'undefined') { 
      newVal = decodeURIComponent(newVal); 
     } 
     if (key in result) { 
      oldVal = result[key]; 
      if ($.isArray(oldVal)) { 
       oldVal.push(newVal); 
      } 
      else { 
       result[key] = [oldVal, newVal]; 
      } 
     } 
     else { 
      result[key] = newVal; 
     } 
    } 

    // Done 
    return result; 
} 

用法:

var pg2 = $.splitQueryString(location.search).pg2; 
相關問題