Forum Moderators: open

Message Too Old, No Replies

search() problem

error saying() search is not a function

         

Seachmall

11:40 am on Oct 27, 2007 (gmt 0)

10+ Year Member



I'm trying to create a code that will look for an anchor in the url of the current location however every time I use the search statment it tells me its not a function.

var currloc = window.location;
alert(currloc.search"#");

Seachmall

12:32 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Sorry, that should be

search("#")

Problem still presists

Little_G

12:41 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Hi,

try:
alert( currloc.substr( currloc.indexOf("#") ) );

Andrew

Seachmall

1:10 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Its now saying indexOf is not a string. Maybe its an error is my overall code but I can;t see anything, can you?

function jump(callsheet){
var currloc = window.location;
var findhash = currloc.substr( currloc.indexOf("#"));
if(findhash==-1){
var windowloc = window.location.split("#");
window.location = windowloc[0] + "#CS" + callsheet;
}else{
window.location = window.location+"#CS" + callsheet;
}
}

It basically checks to see if there is a hash in the url. If there is it splits it into an array. It then takes the part of the array which has everything before the hash and adds the anchor onto it.

If the url has no hash it just adds the anchor.

This where the function is called from:


<select name="callsheet" id="callsheet" class="form" onChange="jump(this.selectedIndex+1)">
<option value="1" id="1">1</option>
<option value="2" id="2">2</option>
</select>

scriptmasterdel

1:16 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Added "currloc = currloc.toString();" to convert it into a string, this should work just fine for you.

function jump(callsheet) {
var currloc = window.location;
currloc = currloc.toString();
var findhash = currloc.substr( currloc.indexOf("#"));
if (findhash == -1) {
var windowloc = window.location.split("#");
window.location = windowloc[0] + "#CS" + callsheet;
} else {
window.location = window.location+"#CS" + callsheet;
}
}

scriptmasterdel

1:19 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



After a little more research you could optionally use:

"var currloc = window.location.href;" instead of converting it into a string.

scriptmasterdel

1:21 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Also, you can control the information about the current location using some preset methods:

Take a look here: [developer.mozilla.org...]

Seachmall

1:24 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Thanks, your first code worked fine, I'll take a look at the link.

If the script wasn't reading currloc as a string what was it reading it as?

Seachmall

1:35 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



I just remebered typeof(). Its an object. :) Thanks again.

Seachmall

1:46 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Sorry for triple post but just incase anyone is interested I changed the entire function (the one seen above) to window.location.hash = "CS"+callsheet;

Thanks again for the link.

scriptmasterdel

5:35 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



No worries, glad you found it useful!