简体   繁体   English

使用变量创建href链接的格式是什么?

[英]What is the format for creating an href link using a variable?

I want my href link to be based on domain names that may change but always utilize the same page name, about.html. 我希望我的href链接基于可能会更改但始终使用相同页面名称about.html的域名。

The javascript code below identifies whether the domain should be abc.com or xyz.com. 下面的javascript代码标识该域应为abc.com还是xyz.com。 My problem is that I do not know how to write the HTML code so that the href will equal: the referring link of abc or xyz plus about.html. 我的问题是我不知道如何编写HTML代码,以使href等于:abc或xyz的引用链接以及about.html。

Javascript Java脚本

$(".abcORxyz").html(document.referrer);

HTML HTML

<a href="What do I need to put here? + .about.html">Other About Page</a>

You don't need to put anything in the href attribute at all. 您根本不需要在href属性中放置任何内容。 You seem to be using jQuery, so you can use jQuery to set the href attribute upon page load. 您似乎正在使用jQuery,因此可以使用jQuery在页面加载时设置href属性。

jQuery jQuery的

$(document).ready(function() {
    $("#aboutLink").attr("href",document.referrer + ".about.html");
});

HTML HTML

<a id="aboutLink" href="#">Other About Page</a>

As a side note, I encourage you not to use document.referrer for this purpose, because you never know what that referrer actually is going to be. 作为附带说明,我鼓励您不要为此目的使用document.referrer ,因为您永远不知道该引用者实际上是什么。 What if Google is what landed the visitor on the page you're using this on? 如果Google是让访问者进入您正在使用此页面的页面,该怎么办? The link would end up looking something like https://www.google.com/search?q=about&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a.about.html . 该链接最终看起来像https://www.google.com/search?q=about&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a.about.html

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

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