str.indexOf issue in form

Discussion in 'JavaScript' started by jianxin9, May 5, 2007.

  1. #1
    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 works to a point, but I cannot figure out how to set the "theright = str.indexOf("genre") + 13;" 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?
    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>

























    </body>
    </html>
     
    jianxin9, May 5, 2007 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Presumably you are using the right property. document.referrer (if available) will give you the url (and parameters if any) used to load the site that loaded yours. location.search holds the parameters used to load your site.

    Either way you're making things too complicated.

    If you use location.search remove the leading '?' using the substring or slice methods.
    Use myString.split('&') to get an array of strings in the form parameter=value, then you could loop through the elements using myString().split on each, to check the name against a list, and in the event of a match, write the value into the corresponding form field.
     
    Logic Ali, May 6, 2007 IP
  3. jianxin9

    jianxin9 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you so much for your help. Do you happen to know where I can find a good example of what you mentioned? I have looked on the web and am not having any luck. I am new to javascript, and I am not familiar with how I would incorporate myString().split

    Thanks so much.
     
    jianxin9, May 7, 2007 IP