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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howto Cut a URL string in an array? 1

Status
Not open for further replies.

RedLion

Programmer
Sep 13, 2000
342
NL
Hello,

I've got the URL: preview.html?name=Mr.%20Smit&Adres=Firststreet%205
now the page has to be look like:

Name: Mr. Smit
Adres: Firststreet 5


Now the problem, how to do that?


<html>
<body>
Name:
<SCRIPT LANGUAGE=javascript>
var s = document.location.search;
s = s.replace(/\?name=/, &quot;&quot;);
s = s.replace(/%20/g, &quot; &quot;);
s.split(&quot;\&&quot;);
s[1] = s[1].replace(/adres/, &quot;&quot;);
document.write(s[0]);
document.write(&quot;<BR>&quot; + &quot;Adres:&quot; + s[1])
//-->
</SCRIPT>
</BODY>
</HTML>


It's nice, but it doesn't work. So please help?

Thanks Charl
 
you want to use window.location.search; this will return a string that looks like this:

string looks like: ?name=snarf&pants=red&weight=50

then just split the string on &quot;&&quot; and you will receive the following strings in the splitstring array:

?name=snarf :use replace to kill the &quot;?&quot; and split &quot;=&quot;
pants=red :split on &quot;=&quot;
weight=50 :split on &quot;=&quot;

I have a library for handling this, if you want to use/see it...

jared@aauser.com
 
Dear Charl,

Here is a javascript object that will handle the querystring for you:

/*
javascript class declaration QueryString
This version does not support arrays of variables, however could
be modified to support arrays.

Copyright (c) Saber Consulting 1999, All Rights Reserved World Wide
*/
function JQueryString(){

this.values = new Array();
this.names = new Array();

this.qs = unescape(document.location.search);
this.qs = this.qs.substr(1);
this.values = this.qs.split(&quot;&&quot;);
for(nqs=0; nqs<this.values.length; nqs++){

var a = this.values[nqs].split(&quot;=&quot;);
this.names[nqs] = a[0].toLowerCase();
}

this.get = jqs_get;

function jqs_get( varName){

if ( 'undefined' == typeof(varName)) varName = new String(&quot;&quot;);
varName = varName.toLowerCase();

var nIndex = -1;
for(n=0; n<this.names.length && -1 == nIndex; n++){
// if they match set nIndex
if ( this.names[n].indexOf(varName) == 0 &&
this.names[n].length == varName.length) {
nIndex = n;
}
} // for())

// if found... split out the value and return it
if ( nIndex > -1){
var a = this.values[nIndex].split(&quot;=&quot;);
return new String(a[1]);
}

return &quot;&quot;;
}
} // JQueryString


You would use it like so...

<SCRIPT LANGUAGE=javascript>
<!--
var q = new JQueryString();
document.writeln( &quot;Name: &quot; + q.get(&quot;name&quot;) + &quot;<br>&quot;);
document.writeln( &quot;Address: &quot; + q.get(&quot;address&quot;) + &quot;<br>&quot;);
//-->
</SCRIPT>

Hope this helps
-pete
 
Of course it helps.
Thanks your the best Pete, now you've realy helpt me with the total solution!!!!!!!!!!!!!!!

By the way the javaScript bible on your site, is it a good book? Is it a reference guide or a real book?

Charl
 
> By the way the javaScript bible on your site, is it a good book? Is
> it a reference guide or a real book?

It 'was' a good book in it's time. It still might have some value but I would be skeptical unless there is a newer version available.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top