We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Add a querystring to an URL with a JAVA-script...

ullenulle
ullenulle asked
on
Medium Priority
303 Views
Last Modified: 2012-05-11
Hi there.

I have a problem. I want to add one or two querystrings to the present URL on a page. I tried making a function like this in the head-section of the page (classic asp):

<%Thispage ="https://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?"%>
<%Thispage2 ="https://" & Request.ServerVariables("SERVER_NAME")%>
<%Thispage3 =request.ServerVariables("URL") & "?"%>
<%Thisquerystring = Request.Querystring("projekt_id_expenses")%>
<SCRIPT language=JavaScript>
function reload(){
var val=form1.samarbejdspartner_expense.value;
var thisurl2=document.write('<%= Thispage2 %>');
var thisurl3=document.write('<%= Thispage3 %>');
var thisquery=document.write('<%= Thisquerystring %>');
self.location=thisurl2 + thisurl3 + thisquery + '&' + 'partner=' + val;
}
</script>

When the function is activated, I get this URL:

https://www.mypage.com//NaN&partner=Nuklearmedicin

and the text: Bad Request.

It added the querystring as I wanted, but it seems that it can't extract the page-name from the URL. I tried extracting the URL-parts, and they seems correct with a document.write, but it obviously doesn't work in JAVA. Can I make the whole thing in JAVA? Please help. I'm not that good at Java yet...

Best regards

Ullenulle
Comment
Watch Question

try self.location=''+thisurl2 + thisurl3 + thisquery + '&' + 'partner=' + val;

Author

Commented:
Only one " ?? Doesn't work...
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
try this:

<%Thispage ="https://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & Request.ServerVariables("SCRIPT_NAME") & "?"%>
<%Thispage2 ="https://" & Request.ServerVariables("SERVER_NAME")%>
<%Thispage3 =request.ServerVariables("URL") & "?"%>
<%Thisquerystring = Request.Querystring("projekt_id_expenses")%>
<SCRIPT language=JavaScript>
function reload(){
var val=form1.samarbejdspartner_expense.value;
var thisurl2=document.write('<%= Thispage %>');
var thisurl3=document.write('<%= Thispage3 %>');
var thisquery=document.write('<%= Thisquerystring %>');
self.location=thisurl2 + 'projekt_id_expenses=' + thisquery + '&' + 'partner=' + val;
}
</script>

Author

Commented:
Hi Big Daddy.

I got another "Bad Request" and the URL got to look like this:

https://www.mypage.com/undefinedprojekt_id_expenses=undefined&partner=Nuklearmedicin

It seems that Java can't figure out to read the Server-info. Any idea how to make the Server-info inside the script?

Best regards

Ullenulle

Author

Commented:
No more suggestions to this question? :-)
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
try this:

<%Thispage ="https://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & Request.ServerVariables("SCRIPT_NAME") & "?"%>
<%Thispage2 ="https://" & Request.ServerVariables("SERVER_NAME")%>
<%Thispage3 =request.ServerVariables("URL") & "?"%>
<%Thisquerystring = Request.Querystring("projekt_id_expenses")%>
<SCRIPT language=JavaScript>
function reload(){
var val=form1.samarbejdspartner_expense.value;
var thisquery='<%= Thisquerystring %>';
self.location=thisurl2 + 'projekt_id_expenses=' + thisquery + '&' + 'partner=' + val;
}
</script>

Author

Commented:
I still get the same error... "Bad request"... :-(   The URL looks like this:

https://www.mypage.com/undefinedprojekt_id_expenses=23&partner=KIA

I want it to look like this:

https://www.mypage.com/pagename.asp?projekt_id_expenses=23&partner=KIA

Best regards

Ullenulle

Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
ok, looks like we're almost there, and I see that I forgot to set thisUrl2 in my last code snippet. so lets try:

<%Thispage ="https://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & Request.ServerVariables("SCRIPT_NAME") & "?"%>
<%Thispage2 ="https://" & Request.ServerVariables("SERVER_NAME")%>
<%Thispage3 =request.ServerVariables("URL") & "?"%>
<%Thisquerystring = Request.Querystring("projekt_id_expenses")%>
<SCRIPT language=JavaScript>
function reload(){
var val=form1.samarbejdspartner_expense.value;
var thisurl2=document.write('<%= Thispage %>');
var thisquery='<%= Thisquerystring %>';
self.location=thisurl2 + 'projekt_id_expenses=' + thisquery + '&' + 'partner=' + val;
}
</script>

Author

Commented:
I'm sorry... but I get the same error again... :-(((
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
what does the url look like?

Author

Commented:
It's coming up with that "undefined" in the URL again:

https://www.mypage.com/undefinedprojekt_id_expenses=23&partner=KIA

Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
can you post your new code set that we've been using, just to make sure we're on the same page?

Author

Commented:
<%Thispage ="https://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & Request.ServerVariables("SCRIPT_NAME") & "?"%>
<%Thispage2 ="https://" & Request.ServerVariables("SERVER_NAME")%>
<%Thispage3 =request.ServerVariables("URL") & "?"%>
<%Thisquerystring = Request.Querystring("projekt_id_expenses")%>
<SCRIPT language=JavaScript>
function reload(){
var val=form1.samarbejdspartner_expense.value;
var thisurl2=document.write('<%= Thispage3 %>');
var thisquery='<%= Thisquerystring %>';
self.location=thisurl2 + 'projekt_id_expenses=' + thisquery + '&' + 'partner=' + val;
}
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
change

var thisurl2=document.write('<%= Thispage3 %>');

to

var thisurl2='<%= Thispage %>';

Author

Commented:
I get "bad request" again... I guess it may not be the best way to do this with Java... so I wrote the full URL in the script and added the query like this:

function reload(){
var val=form1.samarbejdspartner_expense.value;
var thisurl2=document.write('<%= Thispage3 %>');
var thisquery='<%= Thisquerystring %>';
self.location='https://www.mypage.com/pagename.asp?' + 'projekt_id_expenses=' + thisquery + '&' + 'partner=' + val;
}

This way it works the way I want it to... I still think it ought to work the other way too somehow...

Best regards

Ullenulle
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Be back with final feedback tomorrow. :-)
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
Ding, Dong! :)

Author

Commented:
Sorry for my late feedback. Too busy... :-( Thank you for your help!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.