使用带有部分地址的 if 语句来寻址链接
尝试找出一种方法,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽量不要混合 HTML 和 JavaScript:
或者更详细地说:
脚本最好应该放在一个单独的文件中。您建议的方式感觉像 PHP 或 JSP,但 JavaScript 不能以这种方式工作。在上面的示例中,您首先呈现空链接,然后更改
href
属性。Try not mixing HTML and JavaScript:
or more verbosely:
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.我想你想要:
I think you want:
您需要 getElementById
HTML:
Javascript
You need getElementById
HTML:
Javascript
您必须向我们展示此代码在您的页面中执行的位置/时间。您不能像以前那样将 HTML 放入 javascript 的中间。
如果这是内联脚本,您可以调用:
将 HTML 插入到文档中的当前位置。
如果此代码未在文档中内联执行,则不应使用
document.write()
因为这会清除文档并开始新文档。相反,您可以使用 DOM 操作函数将其插入页面中的适当位置或更改现有链接上的 href。例如,当您有以下 HTML 时,要更改现有链接上的 href:您将使用必须在页面加载后运行的 JavaScript:
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:
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:You would use this javascript that must run after the page has been loaded: