View Full Version : Get text out of location

05-08-2007, 10:21 PM
I would like to get the string "palli" out the webpages location:
and then write it into the webpage. This would then be repeated with other strings. Any ideas?

Philip M
05-08-2007, 10:28 PM
var temp = "webpage.com/?i=palli&w=stebbi&re=dagsetning&rada=012";
temp = temp.replace(/palli/i,"");
alert (temp);

05-08-2007, 10:40 PM
the thing is. palli was just an example. I'm runnig script and I need to get just the i= field in the location into the template file? I hope I'm explaining myself well enough.

05-08-2007, 10:43 PM
I think marringi wants to get the string "palli" from the url, not out of the url.
This could be easily done using a server-side language.

05-08-2007, 10:47 PM
I tossed together this example for one way it can be done. It's not optimized by any means but hopefully can give you some ideas.

<title>Parse Query String</title>
<script type="text/javascript">
function doIt() {
var s = "";
var adiv = document.getElementById("adiv");
var q = document.getElementById("q").value;
var v = document.getElementById("v").value;
var re = /\?(.*)$/;
var qstr = null;
var result = re.exec(q);

if (result && result[1]) {
qstr = result[1];

if (qstr) {
var pairArray = qstr.split("&");

for (var i = 0; i < pairArray.length; i++) {
var keyValArray = pairArray[i].split("=");
if (v == keyValArray[0]) {
s = keyValArray[1];

adiv.innerHTML = s;
<label>URL <input type="text" id="q" size="100" value="webpage.com/?i=palli&w=stebbi&re=dagsetning&rada=012"/></label>
<label>Value of <input type="text" id="v" size="100" value="i"/></label>
<button onclick="doIt();">Do it</button>
<br />
<div id="adiv"> </div>


05-08-2007, 10:59 PM
I think the url is the current url of the page. And as I said, if there is a server-side language used, this could be done much easier than in javascript.

If server-side language is PHP:

<?php echo $_GET["i"]; ?>

If VBScript (ASP):


05-08-2007, 11:03 PM
If it is the current url then I believe you can replace the qstr stuff with

var qstr = location.search.substring(1);


05-08-2007, 11:04 PM
this is a CGI/Perl script. I'm displaying result from a database on template pages. Do you have the code for that?

05-08-2007, 11:20 PM
If you want to grab the name of the DB from the URL string, then it is possible in theory with js, but I think it would take some thinking to nail down the substring.

Something along the lines a creating a dummy <span> tag with an id, then within the span tag use document.write to write the location url into the span tag, then using the getElementById it might be possible to use substr(value1,value2) to identify the mydatabase call in the url. Might get messy, but possible.

Is this something that would take a lot of time and energy?

05-08-2007, 11:35 PM
as someone above stated, it would be alot easier to do it with a server side script, but if you really want a javascript version, I have posted a response to a similar question here: http://www.codingforums.com/showthread.php?t=100579

05-08-2007, 11:41 PM
the thing is I don't know how I would use server-side language fix this problem. He talk's about PHP and ASP and I'm using Perl.

05-09-2007, 01:17 AM
I don't know CGI/Perl but here's what I found.

my $param_i = $cgi->param('i');

More info here (http://inconnu.isu.edu/~ink/perl_cgi/lesson2/get_post.html).

05-09-2007, 01:24 AM
To use that you'd have to

use CGI;

to get the module loaded.