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

发布于 2024-12-02 15:55:43 字数 888 浏览 0 评论 0原文

尝试找出一种方法,使用 Javascript 来设置一些 if-else 语句,仅使用 url 的一部分来确定链接是否应该转到一个地方或另一个地方。到目前为止我得到的是,

 <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>

问题是链接甚至没有出现,所以我不知道我错了多少。

感谢您的任何帮助。

编辑:为了论证,我们可以说它是一个空白的 html 页面。如 中所示 <正文>

寻找更多概念证明,然后扩展到在全面的网站上实现此功能。

编辑#2: 想通了,甚至还有网址检测。

<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>

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. 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. As in <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.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掩饰不了的爱 2024-12-09 15:55:44

尽量不要混合 HTML 和 JavaScript:

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

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

或者更详细地说:

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

脚本最好应该放在一个单独的文件中。您建议的方式感觉像 PHP 或 JSP,但 JavaScript 不能以这种方式工作。在上面的示例中,您首先呈现链接,然后更改href属性。

Try not mixing HTML and 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. In the example above you first render empty link and change the href attribute afterwards.

千年*琉璃梦 2024-12-09 15:55:44

我想你想要:

<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>

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>
心如荒岛 2024-12-09 15:55:44

您需要 getElementById

HTML:

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

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>

You need getElementById

HTML:

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

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>
鱼忆七猫命九 2024-12-09 15:55:44

您必须向我们展示此代码在您的页面中执行的位置/时间。您不能像以前那样将 HTML 放入 javascript 的中间。

如果这是内联脚本,您可以调用:

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

将 HTML 插入到文档中的当前位置。

如果此代码未在文档中内联执行,则不应使用 document.write() 因为这会清除文档并开始新文档。相反,您可以使用 DOM 操作函数将其插入页面中的适当位置或更改现有链接上的 href。例如,当您有以下 HTML 时,要更改现有链接上的 href:

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

您将使用必须在页面加载后运行的 JavaScript:

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

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.

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.

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. 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. For example to change the href on an existing link when you have this HTML:

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

You would use this javascript that must run after the page has been loaded:

var link = document.getElementById("myLink");
(url.indexOf("example.com/") != -1) {
    link.href = "placeone.com";
} else {
    link.href = "placetwo.com";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文