PDA

View Full Version : Bang my head against the table! Can you see what i'm missing on two lines of code



Celeste!
Sep 6th, 2007, 02:03 AM
Hello,

Please take a look at my code below. I'm trying to parse out the search terms so i the browser will scroll to those terms. the page containing the following code won't parse the previous URL (which is document.referrer) for the search terms:



<script type="text/javascript">
function URL2Query()
{
var s = document.referrer;
var query = s.substring(s.indexOf("?"),s.length-1);
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.searchTerm);
if(idx!=-1)
{
contents=contents.replace(queryString.searchTerm,'<span id="highlight">'+queryString.searchTerm+'</span>');
document.body.innerHTML=contents;
document.getElementById('highlight').scrollIntoView();
}
};
</script>

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

chump2877
Sep 6th, 2007, 04:06 AM
1) what is the error you are getting in the JS console? Line number?
2) what is the value of document.referrer (the URL that your program is tripping up on)?