Using an if statment in Javascript HTML with a partial address
Trying to figure out a way to use Javascript to set up a little if-else statement using only part of the url to determine if it should go somewhere or not. So far what I got is,
<script>
if (url==example.com) {
window.open('1stoption','1stoption');
} else {
window.open('2ndoption','2ndoption');
}
The problems so far are that it doesnt count if its exampample.com/whatever or anything like that.
Thanks for any help.
*edit Thanks guys it 开发者_StackOverflow中文版worked but I worded my question wrong, it should be more along the lines of showing a link but having the link go to two separate pages depending on the url. This stuff works and I'm kicking it around to get it to work my way too. Thanks alot.
Instead of making an exact match search the url string for the sub-string that you want to accept.
if(url.search(/example\.com/) > -1)
{
//do 1st option here
}
else
{
//do default case here
}
maybe this is what you want?
<script type="text/javascript">
if (url.indexOf("example.com") != -1) {
window.open('1stoption','1stoption');
} else {
window.open('2ndoption','2ndoption');
}
</script>
This will open window first option
if it finds example.com
anywhere within the url
精彩评论