Getting parameters from the query string does not work ... Javascript

I need to code a web application for a university project. It is something like a calendar using JSP, Javascript, Java, JQuery and SQL. So I came pretty far, but I have a problem with my query strings. I am trying to pass the id of the clicked cell to another page that should search the database for a record with that id. I pass this identifier through a QueryString, but I cannot pull it out. I tried everything. Every little piece of code that was supposed to output the parameters. My last attempt:

    var i = location.search.split('i=')[1];

to check if the parameter was disabled, I tried this, which does not work ...

    var x = document.getElementById("vname");
    x.setAttribute("value",i);

vname ist id of the form input field on this site. Thanks for the help :) EDIT: my identifier is not just Integer, it is something like this "fr21" on Friday 21:00.

I literally tried everything I recommended, but it does not work. If I use firebug on firefox, it says: “ReferenceError: functionxyz not defined”, although this ... I don’t know what to do.

+4
source share
1 answer

Using a type approach is split('i=')not very reliable. It is better to use a regular expression and a filter for a specific query string value, e.g.

<script>
  var matches = location.href.match(/&?i=(\w+)&?/);

  if (matches !== null)
  {
    var id = matches[1];

    alert(id);
  }
</script>

, . , , abc=d&i=123&g=h, script 123.

Edit

/&?i=(\w+)&?/

/

&?

i= =

( , [1]

\w A-Z, a-z, 0-9 _

)

&?

/

+1

Source: https://habr.com/ru/post/1621905/


All Articles