开发者

javascript query string

开发者 https://www.devze.com 2023-02-23 02:01 出处:网络
I am learning how to do a query string what html would you use for the following function on the sending and receiving page to see the result of author?

I am learning how to do a query string what html would you use for the following function on the sending and receiving page to see the result of author?

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]")开发者_开发百科;
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

I found it at: http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx

Are they any good query string examples?


I can't quite tell what you are asking but I guess you're looking for a way to test this method and verify its behavior. Here is what I would do, save these contents in an HTML file:

<html>
  <body>
    <form name="form1">
      Key: <input type="text" name="text1" value="author"/>
      <input type="button" name="button1" value="Test"/>
    </form>
    <script>
      document.form1.button1.onclick = function() {
        alert(getQuerystring(document.form1.text1.value));
      }
      function getQuerystring(key, default_) {
        // your code here...
      }
    </script>
  </body>
</html>

Now you can open your HTML page in a web browser and add a query string like "?author=me&foo=bar". For example, if your file is saved in "C:\tmp\example.html" then your URL should look like this:

file:///c:/tmp/example.html?author=me&foo=bar

The page will show a text field (which says "author" by default) and a button and when you press the button the page will show a popup with the result of running the function with the value you put in the text field. With my example query string, the key "author" should alert "me" and the key "foo" should alert "bar".


http://www.example.com?variable=string&var2=ok&var3=str3

is an example of a query string

<script>
var_this = getQuerystring(var2);
if(var_this == "ok"){
  //do this
}else{
  // do this
}
</script>


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

精彩评论

暂无评论...
验证码 换一张
取 消