Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access URL parameters

Status
Not open for further replies.

palchuri

Programmer
Apr 22, 2005
17
0
0
US
Hi All,
I did some research on the internet before posting the question on this forum. I didn't find useful link. Any help is appreciated.

javascript function handleOnClose is invoked on body onbeforeunload. In that I would like access value for the action parameter the url 'AControlServlet?Action=Links&link_id=71'. Can I access?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script>
function popUpWin(url, name, width, height, scroll) {

var properties = "scrollbars=" + scroll + ",width=" + width + ",height=" + height+",left="+((screen.width/2)-(width/2))+",top="+((screen.height/2)-(height/2));
window.open(url, name, properties);
}

function handleOnClose() {
alert("123");
// Here I want to access the value for Action Parameter of the URL
}
</script>
</HEAD>
<BODY onbeforeunload="handleOnClose()">
<a href="javascript:popUpWin('AControlServlet?Action=Links&link_id=71','spsa', 800, 600)">Testing</a>
</BODY>
</HTML>

 

I tried , I am getting the value of Action parameter as null. Here is how I did.

function handleOnClose() {
alert("123");
// I want to access the value for Action Parameter of the URL
alert("value="+ new Querystring().get("Action"));


}

Am I doing something wrong?

Shri
 
I don't think so QueryString works in this scenario.

In the example it was given reading the parameter from the form submission.
var qs = new Querystring();
var face=qs.get('face','Times New Roman');

Here "face" is one of the form parameter. My requirement is read the parameter from the hyperlink url not with the form parameters.


Shri

 
document.location.search is giving the content of address bar not the URL I am trying to invoke in a popup.

Any other suggestions.
 
You can try this
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
    function popUpWin(url, name, width, height, scroll) {

        var properties = "scrollbars=" + scroll + ",width=" + width + ",height=" + height+",left="+((screen.width/2)-(width/2))+",top="+((screen.height/2)-(height/2));
        window.open(url, name, properties);
    }

     function handleOnClose() {
     	alert(document.getElementsByTagName('a')[0].href);
     	alert(document.getElementById("myAnchorLOL").getAttribute("href"));
     	var tempString = document.getElementById("myAnchorLOL").getAttribute("href");
     	alert("I would then split/parse and have my way with the tempString!);
	       // Here I want to access the value for Action Parameter of the URL
     }
  </script>
 </HEAD>
 <BODY onbeforeunload="handleOnClose()">
  <a id="myAnchorLOL" href="javascript:popUpWin('AControlServlet?Action=Links&link_id=71','spsa', 800, 600)">Testing</a>
  <button onclick="handleOnClose();">test</button>
 </BODY>
</HTML>
 
egh missed a " on the last alert but you get the idea. There are a number of ways to the href value of your anchor tags. The best way would prolly be to give your anchor an ID. Use document.getElementById to acces your anchor then after use getAttribute("href") to access the value you need.
 
May be it is my ignorance. How do I know the user has clicked on that link. Somehow I would be able to identify that action has performed on the function handleOnClose() which will be invoked on onbeforeunload.


Regards
Shri
 
In cases like this I use naming conventions to short cut the need to get more information from some other element. I use a naming convention where I get the info I need from either the ID or the name of the element in question. By the way onclick you can pass the parameter of the element with out having the function get those for you. like onclick="somefunction(this.href, this.id)"; I am assuming this is automatically generated code. Either way I use naming conventions in auto generated and code generated elements as well. Hope this helps.
 
[tt]function handleOnClose() {
[red]//[/red]alert("123");
// Here I want to access the value for Action Parameter of the URL
var s=document.getElementsByTagName("a")[0].href;
var rx=/^.*?[&?]action=(.*?)(&.*?)?$/i;
var aq=rx.exec(s);
if (aq) {
alert("action query : " + aq[1]);
} else {
alert("no action query match");
}
}[/tt]
 
Amendment
Upon reading what I posted, I think the pattern should better be amended to properly function in this particular case of href (not like form action attribute). I highlighted the modification.
[tt] var rx=/^.*?[&?][blue]action=(.*?)[&'"][/blue]/i;[/tt]
This is some weakness in apos and quote ending, but it would suffice for a very broad class of query.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top