PCinvent.info

PCinvent Studio Template

Dec 10 2007
Javascript Get Full URL and Queries Print E-mail
Monday, 10 December 2007


Read More Articles in: Technology>>>Programming

This Article is written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it


Sometimes when you have a very complicate URL, using server side langauge like PHP, ASP, or JSP may not get the URL queries easily. For example, one of my website address I have in my website consist two address where the 1st one is only the praser:

http://www.pcinvent.info/index.php?

option=com_wrapper&Itemid=94

&url=galleries/&g2_itemId=27312

So what I am tring to do is to change the "&g2_itemId=27312" to "?g2_itemId=27312" and then redirect......

 

Most of the server-side programming languages that I know of like PHP, ASP, or JSP give you easy access to parameters in the query string of a URL. JavaScript does not give you easy access. With javascript you must write your own function to parse the window.location.href value to get the query string parameters you want. Here is a small function I wrote that will parse the window.location.href value and return the value for the parameter you specify. It does this using JavaScript's built in regular expressions. Here is the function:

 
<script language="javascript">function gup( name )
{
  name = name.replace(/[[]/,"\\\[").replace(/[]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}</script>
<script language="javascript">
var query_param = gup('g2_itemId');
var runonce = gup('runonce');
if (runonce == "yes") {
}
else if (query_param) {
window.location="http://www.pcinvent.info/index.php?option=com_wrapper&Itemid=94
&url=http://www.pcinvent.info/galleries/?g2_itemId="+query_param+"&runonce=yes";
}
else {
}
 
</script>

Notice that when I do the redirection, I add a query "&runonce=yes" behind the URL. And there is If-statement to determine if this query exist in the current URL or not.  This is because without checking a variable, JavaScript will keep looping the page redirection. So,  if (runonce == "yes") means the Script had run once, and it should not run any more.
Enjjoy!
Comments (4)Add Comment
...
written by Mr. Unknown, December 13, 2007
Thanks! This is very useful! It saves my life actually. smilies/cry.gif
...
written by Andy Ng, December 14, 2007
I am glad that it helps you! smilies/kiss.gif
...
written by Florida web design, March 09, 2009
But is there a way to just return everything after the ? as a string?
...
written by Andy, March 10, 2009
Is it necessary to be JavaScript on your task?
You may just use PHP session Get to get the query after the ?
It will be few lines instead of doing JavaScript in which require you to apply the Regular Expression.
Let me know if Client side language is a must like what I did above.

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley

busy
Tags:  Technology Programming Get Full URL Queries Query web address JavaScript Function
Last Updated ( Tuesday, 11 December 2007 )
 
< Prev   Next >
Full Color Printing from PsPrint
Site by PCinvent.com