PDA

View Full Version : str.indexOf issue in form



jianxin9
05-07-2007, 02:07 PM
Hello, I am new to javascript and I am trying to create a form that will pull
data from the referring URL and stick parts of it in the appropriate text boxes.
It is working very well to a point, but I cannot figure out how to set the "theright = str.indexOf("");" appropriately. Basically, I need to pull certain bits of info from the referring URL--but they will not be in the same order every time (that would be so much easier). In this example, I need to pull the info after "genre" and before the next "&". I am having trouble setting the limits on the right--I am telling it to pull "x" number of letters which may or not be enough. Basically, for each section of info I need to pull I need it to stop on the next instance of "&". Is there any way to do this without re-doing everything? It's working great except for this.
Thanks very much for any help.


<strong>Please scroll down and submit the form. Do not edit any information below this point.</strong>
<br /><br />

<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate(str){

theleft = str.indexOf("genre") + 6;
theright = str.indexOf("genre") + 13;
return(str.substring(theleft, theright));
}
document.write("Genre :" +delineate(text));

</SCRIPT>


<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate2(str){

theleft = str.indexOf("atitle") + 7;
theright = str.indexOf("atitle") + 37;
return(str.substring(theleft, theright));
}
document.write("Article Title :" +delineate2(text));

</SCRIPT>

<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate3(str){

theleft = str.indexOf("&title") + 7;
theright = str.indexOf("&title") + 37;
return(str.substring(theleft, theright));
}
document.write("Title :" +delineate3(text));

</SCRIPT>

<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate4(str){

theleft = str.indexOf("issn") + 5;
theright = str.indexOf("issn") + 13;
return(str.substring(theleft, theright));
}
document.write("ISSN :" +delineate4(text));

</SCRIPT>


<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate5(str){

theleft = str.indexOf("date") + 5;
theright = str.indexOf("date")+ 15;
return(str.substring(theleft, theright));
}
document.write("Date :" +delineate5(text));

</SCRIPT>

<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate6(str){

theleft = str.indexOf("spage") + 6;
theright = str.indexOf("spage") + 9;
return(str.substring(theleft, theright));
}
document.write("Page:" +delineate6(text));

</SCRIPT>


<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate7(str){

theleft = str.indexOf("volume") + 7;
theright = str.indexOf("volume") + 10;
return(str.substring(theleft, theright));
}
document.write("Volume:" +delineate7(text));

</SCRIPT>

<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate8(str){

theleft = str.indexOf("issue") + 6;
theright = str.indexOf("issue") + 9;
return(str.substring(theleft, theright));
}
document.write("Issue:" +delineate8(text));

</SCRIPT>


<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate9(str){

theleft = str.indexOf("aulast") + 7;
theright = str.indexOf("aulast") + 17;
return(str.substring(theleft, theright));
}
document.write("Author Last Name:" +delineate9(text));

</SCRIPT>


<SCRIPT LANGUAGE="javascript">

var locate = document.referrer
document.form1.value = locate

var text = document.form1.value

function delineate10(str){

theleft = str.indexOf("aufirst") + 8;
theright = str.indexOf("aufirst") + 18;
return(str.substring(theleft, theright));
}
document.write("Author First Name:" +delineate10(text));

</SCRIPT>

<br /><br />
<strong>Genre:</strong><input type="text" name="genre" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork = delineate(text)
document.form1.genre.value = zork
</SCRIPT>

<br /><br />
<strong>Article Title: </strong><input type="text" name="article" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork2 = delineate2(text)
document.form1.article.value = zork2
</SCRIPT>

<br /><br />
<strong>Title: </strong><input type="text" name="title" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork3 = delineate3(text)
document.form1.title.value = zork3
</SCRIPT>

<br /><br />
<strong>ISSN:</strong> <input type="text" name="issn" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork4 = delineate4(text)
document.form1.issn.value = zork4
</SCRIPT>

<br /><br />
<strong>Year:</strong> <input type="text" name="year" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork5 = delineate5(text)
document.form1.year.value = zork5
</SCRIPT><br /><br />


<strong>Starting Page: </strong><input type="text" name="page" size="50" />

<SCRIPT LANGUAGE="javascript">
var zork6 = delineate6(text)
document.form1.page.value = zork6
</SCRIPT>
<br /><br />


<strong>Volume: </strong><input type="text" name="volume" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork7 = delineate7(text)
document.form1.volume.value = zork7
</SCRIPT><br /><br />


<strong>Issue: </strong><input type="text" name="issue" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork8 = delineate8(text)
document.form1.issue.value = zork8
</SCRIPT><br /><br />


<strong>Author Last Name: </strong><input type="text" name="aulast" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork9 = delineate9(text)
document.form1.aulast.value = zork9
</SCRIPT><br /><br />


<strong>Author First Name: </strong><input type="text" name="aufirst" size="50" />
<SCRIPT LANGUAGE="javascript">
var zork10 = delineate10(text)
document.form1.aufirst.value = zork10
</SCRIPT>

<br /><br />

<strong>Page URL:</strong> <input type=text name="theAddr" size=50 /><br /><br />

<input type="submit" value="Submit request">

</form>
</body>
</html>


<br><br>

jscheuer1
05-07-2007, 02:28 PM
It sounds like to me that you are pulling from the query:

www.somedomain.com/index.htm?genre=jazz&artist=miles_davis&album=kind_of_blue

And that you cannot always know the sequence of things in the query or if there might be other things there that you aren't even looking for and that some of the things you are looking for might not be there all of the time.

If that is about the situation, a user here (Dimx, I think), came up with this routine that works well for that sort of thing:


function get(key_str) {
var query = window.location.search.substr(1);
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
if(unescape(pair[0]) == key_str)
return unescape(pair[1]);
}
return null;
}

For example, if you wanted the genre value (from the query if it is there) in a textbox named 'genre' in a form named 'music':


document.forms['music'].elements['genre'].value=get('genre')? get('genre') : "";

However, this sort of thing is really meant to be done server side. So, if your host has a server side language available to you (like PHP or asp) you should probably use that.

mwinter
05-07-2007, 03:52 PM
Rather than use all of those delineate derivative functions, use the following instead. You'll only ever need one copy of it.



function getQueryValue(name) {
var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.referrer);

return match ? unescape(match[1]) : null;
}

To get the value associated with 'genre', write:



getQueryValue('genre')

If present, the return value will be a string. If not, the value will be null.

Whoever told you that to convert a value to a string, one needs to assign a variable to a form control, then back to another variable hasn't a clue about browser scripting. Don't ever listen to them again. The best approach is:



String(value)

but it's unnecessary here as the referrer property already is a string.



Is there any way to do this without re-doing everything?

You really should restart. The document is far longer and more complicated than it needs to be. Consider the following fragment:



<head>
<title>...</title>

<script type="text/javascript">
function getQueryValue(name) {
var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.referrer);

return match ? unescape(match[1]) : null;
}

function populateFields() {
var controls = document.forms.form1.elements,
fieldMap = {genre: 'genre', article: 'atitle', title: 'title', issn: 'issn',
year: 'date', page: 'spage', volume: 'volume', issue: 'issue',
aulast: 'aulast', aufirst: 'aufirst'};

for (var i = 0, n = controls.length; i < n; ++i) {
var value = getQueryValue(fieldMap[controls[i].name]);
if (value) controls[i].value = value;
}
}
</script>
</head>

<body onload="populateFields();">
<form id="form1" name="form1" action="...">
<div>
<label>Genre: <input name="genre" type="text"></label><br>
<label>Article: <input name="article" type="text"></label><br>
<!-- ... -->
</div>
</form>
</body>






function get(key_str) {
var query = window.location.search.substr(1);
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
if(unescape(pair[0]) == key_str)
return unescape(pair[1]);
}
return null;
}


That operates on the location object, not the referrer property.



However, this sort of thing is really meant to be done server side.

I'm inclined to agree, mainly because the message accompanying the form says not to edit the values, which is a little odd and rather defeats the point of a form.

Mike

jscheuer1
05-07-2007, 04:36 PM
That operates on the location object, not the referrer property.

Yes, I was just scanning the post and missed that. Odd that one would want to grab values from the referrer, but if that is what you need, Mike's method looks good.

You are still better off with a server side solution, if available.

jianxin9
05-07-2007, 05:05 PM
Thanks to both of you for taking the time to respond. I really appreciate it.

Twey
05-07-2007, 05:58 PM
I was under the impression that escape() and unescape() were deprecated for encodeURI()/encodeURIComponent() and decodeURI()/decodeURIComponent() (the latter in this case).

mwinter
05-07-2007, 06:07 PM
I was under the impression that escape() and unescape() were deprecated for encodeURI()/encodeURIComponent() and decodeURI()/decodeURIComponent() (the latter in this case).

The escape and unescape functions aren't formally specified by ECMA-262, though a suggested implementation is included in the third edition. However, older versions of IE add compatibility problems - making escape an alias for encodeURI, for example, isn't necessarily adequate. It's certainly possible to counter this problem for IE (and any other browser), but that would have made my previous post rather long. Besides, I dragged that code out from the archives. :)

Mike