0

I am trying to display data from an external .jsp file, which is set up something like this:

<tag>
  <innertag1 id="1">
  <innertag1 id="2">
</tag>
<tag>
  <innertag2 id="3">
  <innertag2 id="4">
</tag>

To display only information from only one particular "innertag" tag, I'm currently using:

NodeList labs = XMLInfo.getElementsByTagName("innertag1");

I'd like to be able to isolate any particular tag with ease. Theoretically, I could create many individual pages and simply change the values to "innertag2," "innertag3," etc., but this is obviously a bit impractical.

Is there a way to determine the value via a URL parameter? For instance, if I wanted to only display data from "innertag2," is there a way that the url http://www.server.com/data.jsp?id=innertag2 would adjust the tagname properly?

Thank you, any help would be much appreciated.

  • I'm guessing this would be pretty easy with jQuery's selector – JCOC611 Jan 27 '11 at 22:30
  • Just as a note, real "id" values should look like identifiers (they can't start with a digit, in other words) – Pointy Jan 27 '11 at 22:30
  • 1
    I don't make up the rules, I just type them into comments here :-) That's just what the spec says. – Pointy Jan 27 '11 at 22:40
  • Pointy, I got you. When writingthe example coding, I tried to make it generic and ended up accidentally doing that. Thanks – Aaron Weber Jan 27 '11 at 22:41
  • 1
    @Juan Mendes - Here's the [HTML 4.01 statement on the matter](http://www.w3.org/TR/html401/types.html#type-name): 'ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").' – Pointy Jan 27 '11 at 22:41
  • That's cool, I had never heard of that! – Ruan Mendes Jan 27 '11 at 22:44

2 Answers2

2

You can parse document.location.href and extract parameters from there. This is from an old HTML file where I used this technique (not sure if it's compatible on all browsers, however).

var args = {};

function parseArgs()
{
    var aa = document.location.href;
    if (aa.indexOf("?") != -1)
    {
        aa = aa.split("?")[1].split("&");
        for (var i=0; i<aa.length; i++)
        {
            var s = aa[i];
            var j = s.indexOf("=");
            if (j != -1)
            {
                var name = s.substr(0, j);
                var value = s.substr(j + 1);
                args[name] = value;
            }
        }
    }
}
6502
  • 112,025
  • 15
  • 165
  • 265
  • I've always heard it called window.location, never document.location, but it works. Also location.search saves you some work. – Ruan Mendes Jan 27 '11 at 22:38
1

Not sure if this is what you're looking for, but you can access parameters from the url using location.search.

6502's answer is almost good enough, it's not url decoding parameters. The function below is a bit more polished (descriptive variable names, no global variables)

function getUrlParams() {

  var paramMap = {};
  if (location.search.length == 0) {
    return paramMap;
  }
  var parts = location.search.substring(1).split("&");

  for (var i = 0; i < parts.length; i ++) {
    var component = parts[i].split("=");
    paramMap [decodeURIComponent(component[0])] = decodeURIComponent(component[1]);
  }
  return paramMap;
}

Then you could do

var params = getUrlParams();
XMLInfo.getElementsByTagName(params['id']); // or params.id
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217