原型 - `toQueryParams()` 方法



此方法將解析類似 URI 的查詢字串,並返回由引數/值對組合的物件。此方法類似於 parseQuery()。

此方法實際上以解析查詢字串為目標(因此,分隔符引數的預設值為“&”)。

語法

string.toQueryParams ([separator = '&']);

返回值

返回具有鍵值對的物件。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var str = "http://www.example.com?section=blog&id=45#comments";
            var obj = str.toQueryParams ();
            alert ( "obj.section :  " + obj.section );
            alert ( "obj.id :  " + obj.id );
            
            var str = "tag=ruby%20on%20rails";
            var obj = str.toQueryParams ();
            alert ( "obj.tag:  " + obj.tag );
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br />
      <br />
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

輸出

prototype_string_processing.htm
廣告
© . All rights reserved.