Can a javascript attribute value be determined by a manual url parameter?
Asked Answered
D

2

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.

Depositary answered 27/1, 2011 at 22:25 Comment(6)
I'm guessing this would be pretty easy with jQuery's selectorUnearned
Just as a note, real "id" values should look like identifiers (they can't start with a digit, in other words)Affirmative
I don't make up the rules, I just type them into comments here :-) That's just what the spec says.Affirmative
Pointy, I got you. When writingthe example coding, I tried to make it generic and ended up accidentally doing that. ThanksDepositary
@Juan Mendes - Here's the HTML 4.01 statement on the matter: '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 (".").'Affirmative
That's cool, I had never heard of that!Degrading
P
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;
            }
        }
    }
}
Polynuclear answered 27/1, 2011 at 22:31 Comment(1)
I've always heard it called window.location, never document.location, but it works. Also location.search saves you some work.Degrading
D
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
Degrading answered 27/1, 2011 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.