Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-04-27
19
Medium Priority
?
264 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
0
Comment
Question by:ullenulle
  • 10
  • 6
  • 2
  • +1
19 Comments
 
LVL 6

Expert Comment

by:cfEngineers
ID: 35478562
try self.location=''+thisurl2 + thisurl3 + thisquery + '&' + 'partner=' + val;
0
 

Author Comment

by:ullenulle
ID: 35478774
Only one " ?? Doesn't work...
0
 
LVL 34

Expert Comment

by:Big Monty
ID: 35478855
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>

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ullenulle
ID: 35479876
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
0
 

Author Comment

by:ullenulle
ID: 35716835
No more suggestions to this question? :-)
0
 
LVL 34

Expert Comment

by:Big Monty
ID: 35720686
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>

0
 

Author Comment

by:ullenulle
ID: 35768205
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

0
 
LVL 34

Expert Comment

by:Big Monty
ID: 35776915
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>
0
 

Author Comment

by:ullenulle
ID: 35812785
I'm sorry... but I get the same error again... :-(((
0
 
LVL 34

Expert Comment

by:Big Monty
ID: 35818862
what does the url look like?
0
 

Author Comment

by:ullenulle
ID: 35819631
It's coming up with that "undefined" in the URL again:

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

0
 
LVL 34

Expert Comment

by:Big Monty
ID: 35832498
can you post your new code set that we've been using, just to make sure we're on the same page?
0
 

Author Comment

by:ullenulle
ID: 35873574
<%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;
}
0
 
LVL 34

Expert Comment

by:Big Monty
ID: 35879045
change

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

to

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

Author Comment

by:ullenulle
ID: 35913381
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
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 35936106
try
<%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 type="text/javascript">
function reload(){
var val=form1.samarbejdspartner_expense.value;
var thisurl2='<%= Thispage2 %>';
var thisurl3='<%= Thispage3 %>';
var thisquery='<%= Thisquerystring %>';
self.location=thisurl2 + thisurl3 + thisquery + '?' + 'partner=' + encodeURIComponent(val);
}
</script>


But I think this simpler alternative will also work. Try them both:
<script type="text/javascript">
function reload(){
var val=form1.samarbejdspartner_expense.value;
self.location='<%="https://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?partner="%>' + encodeURIComponent(val);
}
</script>

Open in new window

0
 

Author Comment

by:ullenulle
ID: 36051116
Be back with final feedback tomorrow. :-)
0
 
LVL 82

Expert Comment

by:hielo
ID: 36536249
Ding, Dong! :)
0
 

Author Closing Comment

by:ullenulle
ID: 37173893
Sorry for my late feedback. Too busy... :-( Thank you for your help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question