determining whether a character exists in the address

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello. i want to determine whether the percentage sign (%) exists in the
page's address, so i write this code:

<script language=javascript>
presentAdd = self.location
<!-- "self.location" cannot be replaced by the actual file name because it
will be employed in different pages -->
if (presentAdd.indexOf('%') > -1) {
document.write('present')
} else {
document.write('absent')
}
</script>

IE returns an error message "Object doesn't support this property or method".
What should be modified?
Thanks.
 
if (document.location.href.indexOf('%') > -1) {
document.write('present')
} else {
document.write('absent')
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
Kev's reply is good but if you want to learn javascript for future projects
doing it like this should help

document.write(location.href.indexOf("%")==-1? "absent":"present")
 
Definitely more succinct, Jon. And succint is good!

--

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
Great ,
I like it too

Is it true that (condition) ? value2 : value2 can be used anywhere where one
would normally place value ?

I note that Jon's code does not even place the condition in ()
 
Yes, you can use it like this
var a = 10
b=(a==10)?20:30;
// brackets are optional this would be equally valid
b=a==10?20:30;
//b =20
// or
alert(a==10?'It\'s ten':'It\'s not ten')
 
Back
Top