简体   繁体   中英

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.

The javascript code below identifies whether the domain should be abc.com or 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.

Javascript

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

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. You seem to be using jQuery, so you can use jQuery to set the href attribute upon page load.

jQuery

$(document).ready(function() {
    $("#aboutLink").attr("href",document.referrer + ".about.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. What if Google is what landed the visitor on the page you're using this on? 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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