简体   繁体   English

在Javascript HTML中使用带有部分地址的if语句

[英]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. 试图找出一种方法来使用Javascript来设置一个if-else语句,只使用部分url来确定它是否应该去某处。 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 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. 而不是精确匹配搜索您要接受的子字符串的url字符串。

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 如果在网址内的任何位置找到example.com这将打开first option窗口first option

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM