Jump to content

Help accessing query strings from within the client


trainzebra

Recommended Posts

Hey all. I'm trying to write an xsl file that uses javascript to get query strings that are appended to the url after a button press (no server side scripting). My understanding is that the general procedure for this is to reference window.location.href or window.location.search to get the query string then parse accordingly. However, after my form submits its value the href variable doesn't seem to be updating. Here's what the code for my form looks like.

<form name="filter" method="get" action="">						<select name="list">							<option name="opAll" value="all">All Items</option>							<option name="opCompleted" value="completed">Completed</option>							<option name="opStarted" value="started">Started</option>							<option name="opPending" value="pending">Pending</option>						</select>						<input type="submit" value="Display" onClick="pageEdit(this.form)"></input></form>

After hitting the submit button, my url will look like this for example:file:///C:/Documents%20and%20Settings/...table.xml?list=allIf I script document.write(window.location), it gives the full path to my file, but no query string.file:///C:/Documents%20and%20Settings/...table.xmlIf I output document.write(window.location.href) it's empty. My question is, why is it doing this? Is there any way to simply get the text that's currently displayed in the address bar of my browser? If I need to provide any more code let me know. Thanks for any help.

Link to comment
Share on other sites

make a file called request.js and put this code in it

	var Request = 	{		QueryString: []	}		//load Request.QueryString	var q = window.location.href.split('?')[1];	var pairs = (q != null) ? q.split('&') : [];	for(var i=0;i<pairs.length;i++)	{		var pair = pairs[i].split('=');		Request.QueryString[pair[0]] = (pair != null) ? pair[1] : "";	}

Then you can get querystrings like this

var str = Request.QueryString["varName"];

Link to comment
Share on other sites

Thanks for the help. However it seems to be doing the same thing, when I output str after running the function it's coming up as undefined. I'm assuming it's because the reference to href within the function is turning up empty. Thanks again though, any other ideas?

Link to comment
Share on other sites

file:///C:/Documents%20and%20Settings/...table.xml?list=all
...the window.location.search variable that the function references comes back empty.
Could it be because you are trying to get the query string from a URL for an XML file rather than an HTML one?EDIT: Are you using IE? Have you tried it in Firefox? I bet you are using this in IE and you are running locally. For some reason, location.search doesn't return the query string in IE 6 locally for me, but it does in Firefox. If you ran a webserver at http://localhost or uploaded it to another web server, it might start working. Here's a link to a forum where someone is having a similar problem: http://forums.microsoft.com/MSDN/ShowPost....54&SiteID=1
Link to comment
Share on other sites

I tried it in firefox and it gets a parsing an xslt stylesheet failed message. However I am indeed running it locally using IE6. This sounds like a winner, I'll try uploading it first thing in the morning. Thanks much :)EDIT: I uploaded my file onto localhost on my machine and it does indeed get the query string no problem. However, now I'm running into a problem with my if statements functioning.I've tried a couple different solutions. I'm just going to go with the simplest example I have here with simply an image and no xsl. I want to display the image blue1.jpg when the variable str = "started".When I attempt it this way, the image is displayed no matter what str is:

<script type="text/javascript">if(str == "started"){</script>	<td><img src="blue1.jpg"></img></td><script type="text/javascript">}</script>

When I attempt it this way, there's a conflict between what javascript and xsl want. If I put a backslash before the quotes in blue1.jpg to suppress their meaning, xsl throws an error because it expects a quote after src=. If I don't put them there, javascript reads the first one as the end of the string and doesn't display anything.

<script type="text/javascript">if(str == "started"){	  document.write("<td><img src="blue1.jpg"></img></td>")}</script>

Does anyone know of any way to work around this?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...