Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile easy for YOU... HARD for newbie JS Girl

    Hello,
    I'm trying to parse the referring url for google search terms so that when this page loads it will scroll to and highlight the search term(s). Should i be using document.referrer?

    Please take a look at my code and tell me what i'm doing wrong:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>highlight</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script type="text/javascript">
    function URL2Query()
    {
    var query = document.referrer("?","");
    var Q = query.split("&");
    var hash = {};
    for(var i=Q.length-1;i>=0;--i)
    {
    var q = Q[i].split("=");
    if(q[0]) hash[q[0]]=unescape(q[1]);
    }
    return hash;
    }

    window.onload=function(){
    var queryString = URL2Query();
    var contents=document.body.innerHTML;
    var idx=contents.search(queryString.query);
    if(idx!=-1) {
    contents=contents.replace(queryString.query,'<span id="highlight">'+queryString.query+'</span>');
    document.body.innerHTML=contents;
    document.getElementById('highlight').scrollIntoView();
    }
    };
    </script>

    <style type="text/css">
    #highlight {background:yellow;}
    </style>

    </head>
    <body>
    <script>
    document.write(document.referrer);
    </script>

    <h1>Nirafo</h1>
    <p>Aphasm escit ra apovum ecripsat. Quorepl alist osmephum ime itasmotis to eleh.
    Quadeth est. Coticreus oce ro omuf ari trolodeus est. Nosm est. Thihopsiem,
    minus beh brofiprue teranis. </p>
    <h2>Smopada</h2>
    <h2>Edrecram Ol <a href="highlight2.htm">Quorobucranis</a></h2>
    <h3>Funap</h3>
    <p>Ohusch ifemes, presn mapliant ple pupriem nalaho. Acrutra son cro teton hi
    prenaquid est. Ocophiant asi pso bratetriem eta cragasch est. Volespes in
    aplasnem usnerhant. Gerociant ina quidu tefum ag hiphue est. </p>
    <p>Trog crin maquor on crebatuer tes efascranis. Ehisc pibisnam snatofas, ic
    smot pel vavetont. Drup nicrus quido edesmiem plarodonis. Ugepsiant sosch log
    fathon atahotis. Stim dabr quorimapseus vi dathat ufo lequadat. Quoreh muspeus
    quidicusp, bro psopl tutist ropinuer. Schops tremorh, otusm lusagore dro
    aquorospore pluvusmont. Socelum in rhetapa tumotis. Tof, vufucrue ul uquadicrum
    ulovant. Smotr ruquidi ipreprant, an ugist popupr mucanis. Acron osmac ipeh vav
    iloquader quadi govedas. Das mipacras, scet roteprore tin tabesch opod. Escreh
    quadot dipapam nan hubo bre etomas. Sorhiant, vasta quoro opraquoriant
    meriplotis. </p>
    <p>Apoquid equoraphes mo buthi apretrotis. Fasm ohesmus, phud planerhes pe
    mafothius itohonis. Brigiquorum in imischius vicrat. Smubr est. Orhan ipsev
    tigigue ana omosmer ide tacront. </p>
    <h3>Omorue</h3>
    <p>Escol tosn umequidem vef asmibrer vi poricatis. Rothiem ef ano olotreus ah
    stepephum est. Elicr osnepla baquadont, hes thisp osmicriant quidoteplant.
    Ipricreus in falusmam esmobrat. Losch brebuquadum vi scasuquidam psenerer. Nasm
    trunopsit ac regabue tobiscro. Scropl thabipiant at tequadore raciquidat.
    Thatoscrus psinorat, staquad letore rhu dam moscuer. Gunit spisp to tahius
    abribruer. Rhol brim asarus ahe epifer ega lononont. Stuph laspa ubi spimason
    palanis. Rilebrue in alabon anabruer. Losm eda toh itrismeus schi rismus est.
    Brov trotiphue eni ditob abrecras. </p>
    <p>Scrapabrue in ololum ihaschotis. Uphar est. Soquid oquorephem hec stoniquadus
    catasp. Anosc escruquad ip ihetha listonis. Mes in mirus uhiscanis. </p>
    <h3>Pre Obaquad Nohequorant</h3>
    <p>Thoph rutuphem, schin nobram ah ivascon psahitotis. Esmal quorabiteus ehi
    dimequor cunavanis. Arhel odagus pse ospaquideus psotamont. Metoquidi pre ohe
    vaquidam sce icimius est. Spast thesepus ime gafore apriquadatis. </p>
    <h2>Rhenosmanis</h2>
    <h3>Tah Conedit Drageschat</h3>
    <p>Cril mepam spihecront, im psen scrumirham devath. Ciquid scar ladam thu
    comanatis oro etrusmas. Thol diprem, uthuf piva ob phetepses craramatis. Epirh
    est. Ohasc pufim im otagit matiphas. Osmod locuscam, ulerh givescam alo
    ferespius familonis. Idabreus quorid ha phogemiem imeplont. Osith busnem
    scrapinant, ta craf ethosn losmonis. Pofiem gi ilu lapemem eg opadrius est.
    Breps snesascram, asath abuquadeus ho nophes vupodont. Quorideham, corap et
    scriperem schohuto. Path tesothius noper, dru quadicr atradr ehapsant. Smes
    uton, quidap scerosch tre pibiniant poher. </p>

    </body>
    </html>

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Untested, but this should return you an array of the words they searched for, which can then be looped through and done with what you wish.

    Code:
    function getSearchWords(){
    	var str=document.referrer;
    	var searchwords=new Array();
    	var cut=str.split('&');
    	var len=cut.length;
    	for(var i=0;i<len;i++){
    		var cut2=cut[i].split('=');
    		if(cut[0]=='q'){
    			var cut3=cut[1].split('+');
    			var len2=cut3.length;
    			for(var j=0;j<len2;j++){
    				searchwords[searchwords.length]=decodeURIComponent(cut3[j]);
    			}
    		}
    	}
    	return searchwords;
    }
    Basscyst
    Last edited by Basscyst; 09-05-2007 at 12:38 AM.
    Helping to build a bigger box. - Adam Matthews


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •