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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 754
  • Last Modified:

Read parameters from URL

I'm new at javascript and from what i understand what i'm trying to do is no easy step at all.  I need a javascript that will read the parameters from the URL so i can write them in my table.  The only way i can see being able to do this is by reading the parameters into variables and then using the document.write(variablename); to display the results.  i'd REALLY appreciate all the help i can get here.

Steve.
0
tekguy2001
Asked:
tekguy2001
2 Solutions
 
v2MediaCommented:
"The only way i can see being able to do this is by reading the parameters into variables and then using the document.write(variablename); to display the results" ... is exactly how you'd go about it. Have you not got some code to show?
0
 
gaston_acevedoCommented:
adapt this to your code and try:

var theUrl="http://mysite.org?variable1=a&variable2=2";

var urlPart = theUrl.split("?")[1];//the parameters in a url allways follow the question simbol (?)
var arrparams = urlPart.split("&");//the parameters in a url allways are separated by and &
for(var i=0; i< arrparams.length;i++){
    var nameAndValue = arrparams[i].split("=");//the params allways are pairs separated by a equals(=);
    var name=nameAndValue [0];
    var value=nameAndValue [1];
    alert("Name= "+name+"\nValue= "+value);
}

0
 
gaston_acevedoCommented:
i forggot to say:
the url is accesible using location.href

Good Luck
0
 
Ryan ChongCommented:
You can try function like this:

<script language="javascript">

function querystring(name)   // returns a named value from the querystring
{  //yourpage.html?xyz=123
   var tmp = ( location.search.substring(1) );
   var i   = tmp.toUpperCase().indexOf(name.toUpperCase()+"=");
 
   if ( i >= 0 )
   {
      tmp = tmp.substring( name.length+i+1 );
      i = tmp.indexOf("&");
      return unescape( tmp = tmp.substring( 0, (i>=0) ? i : tmp.length ));
   }
 
   return("");
}

</script>

then use like:

<script language="javascript">

var mypara = querystring("mypara")
alert("mypara = " + mypara);

</script>


if you got url like this:

http://yourdomain.com/yourpage.html?bla1=1&mypara=324234&mypara2=blablah


get the idea?
0
 
tekguy2001Author Commented:
AWESOME!!  Both work, so i'm awarding the points to both.  Thanks for the information.  I have another question posted for as much points as this one.  It's a doozy for me too as now i have to display photo's and it's stored in /lot/paramatername/
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now