简体   繁体   English

使用带有部分地址的if语句来访问链接

[英]Using an if statment with a partial address to adress a link

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 a link should go one place or another. 试图找出一种使用Javascript来建立一个if-else语句的方法,该方法仅使用部分URL来确定链接是否应该放在一个地方或另一个地方。 So far what I got is, 到目前为止,我得到的是

 <script type="text/javascript">
    if (url.indexOf("example.com/") != -1) 
{
        <a href="placeone.com" target="_blank">Blahblah</a>
    } else {
        <a href="placetwo.com" target="_blank">Blahblah</a>
        }
 </script>

The problem is that the link doesn't even appear so I don't know how wrong or not I am. 问题是该链接甚至都没有出现,所以我不知道我是怎么错。

Thanks for any help. 谢谢你的帮助。

Edit: Lets just say for the sake of argument it is a blank html page. 编辑:让我们只是为了论证它是一个空白的html页面。 As in <html> <body> </body> </html> <html> <body> </body> </html>

Looking for more of a proof of concept then branch out into getting this working on a full scale site. 寻找更多的概念证明,然后扩展到让它在全面的站点上运行。

Edit #2: Figured it out, even has url detection. 编辑#2:弄清楚,甚至具有URL检测。

<a id="link">link</a>
<script type="text/javascript">
var link = document.getElementById('link');
var referrerUrl = document.referrer;
if (referrerUrl.indexOf("searchurlfor") != -1)
{
    link.href = "place1";
} else {
    link.href = "place2";
}
</script>

Try not mixing HTML and JavaScript: 尝试不要混合使用HTML和JavaScript:

<a id="someLink" target="_blank">Blahblah</a>

<script>
  document.getElementById('someLink').href = 
    (url.indexOf("example.com/") != -1)? 'placeone.htm' : 'placetwo.htm'
</script>

or more verbosely: 或更详细地说:

<script>
  var linkElem = document.getElementById('someLink');
  if(url.indexOf("example.com/") != -1) {
    linkElem.href = 'placeone.htm';
  } else {
    linkElem.href = 'placetwo.htm';
  }
</script>

Preferably the script should go to a separate file. 最好脚本应转到单独的文件。 The way you suggest feels like PHP or JSP but JavaScript does not work this way. 您建议的方式感觉像PHP或JSP,但JavaScript不能以这种方式工作。 In the example above you first render empty link and change the href attribute afterwards. 在上面的示例中,您首先呈现空白链接,然后更改href属性。

I think you want: 我想你要:

<script>
   if (url.indexOf("example.com/") != -1) 
   {
      document.write('<a href="placeone.com" target="_blank">Blahblah</a>');
   } else {
      document.write('<a href="placetwo.com" target="_blank">Blahblah</a>');
   }
</script>

You'd have to show us where/when this code is executing in your page. 您必须向我们展示此代码在页面中的执行位置/时间。 You can't just drop HTML into the middle of a piece of javascript like you were doing. 您不能像刚才那样将HTML放到一段JavaScript的中间。

You can call: 您可以致电:

document.write('<a href="placeone.com" target="_blank">Blahblah</a>');

to insert HTML into the current place in the document if this is an inline script. 如果这是嵌入式脚本,则将HTML插入文档的当前位置。

If this code is not executing inline in the document, then you should not use document.write() as that will clear your document and start a new one. 如果此代码未在文档中内联执行,则不应使用document.write()因为那样会清除您的文档并开始一个新的文档。 Instead, you would use DOM manipulation functions to insert this into the appropriate place in the page or to change the href on an existing link. 取而代之的是,您将使用DOM操作函数将其插入页面中的适当位置或更改现有链接上的href。 For example to change the href on an existing link when you have this HTML: 例如,当您拥有此HTML时,要更改现有链接上的href:

<a id="myLink" href="placeone.com" target="_blank">Blahblah</a>

You would use this javascript that must run after the page has been loaded: 您将使用此javascript,该javascript必须在页面加载后运行:

var link = document.getElementById("myLink");
(url.indexOf("example.com/") != -1) {
    link.href = "placeone.com";
} else {
    link.href = "placetwo.com";
}

You need getElementById 您需要getElementById

HTML: HTML:

<a id="link">link</a>

Javascript 使用Javascript

<script type="text/javascript">
var link = document.getElementById('link');
if (url.indexOf("example.com/") != -1) 
{
        link.href="placeone.com";
} else {
        link.href="placetwo.com";
}
 </script>

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

相关问题 在Javascript HTML中使用带有部分地址的if语句 - Using an if statment in Javascript HTML with a partial address 单击使用python硒的部分div链接 - Click a partial div link using python selenium gatsby 链接正在使用当前地址而不是使用根地址 - gatsby link is using the current address instead of using the root address 使用link_to_function时,Rails部分会被两次转义 - Rails partial gets double escaped when using link_to_function 使用定位器找不到任何元素:作者(部分链接文本,Solid Community) - No element found using locator: By(partial link text, Solid Community) 使用 <select>带有转换语句的标签(Java和HTML) - Using <select> tags with a Switch statment (Java and HTML) 在if语句中的数组上使用indexOf的结果不一致 - Inconsistent results of using indexOf on an array in an if-statment 在JavaScript条件陈述中使用Razor语法 - Using Razor Syntax within JavaScript Conditional Statment Javascript / Jquery如何检查URL地址是否与主页地址相同,地址中的国家和语言可变? - Javascript/JqueryHow to check url address if its same as home page adress with variable country and language in address? 将Regex vs include()方法与Switch Statment一起使用 - Using Regex vs includes() method with Switch Statment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM