我还没有在这里找到一个很好的例子,所以我希望任何人都可以提供帮助!
我希望仅当我的引荐来源网址等于特定网址和路径时才使用 Javascript 显示文本字符串。 (当然,如果有更好更有效的方法,我洗耳恭听!)
示例:
- 如果引用网址恰好是“https://example.com/thepage ”
- 然后在页面顶部显示 html 文本:“感谢您来到本页面”
- 如果它们来自其他地方,则忽略并且不显示文本
注意:我不想通过 js“警报”显示此内容
谢谢大家的帮助! 乔
最佳答案
您可以在 HTML 上创建一个 div 元素(为其分配 id 或 class)并将其留空,然后在 js 上您可以定位它并在条件返回 true 时为其分配一个值
// target the element
let TargetedElem = document.getElementById("ELEM_ID");
// target Current Path
let path = window.location.pathname;
// if you wanna target the whole URL then`
let URL = window.location.href;
if(path == "YOUR Path") {
TargetedElem.innerText = "YOUR MESSAGE";
}else {
TargetedElem.innerText = null;
}
关于Javascript - 当我的引荐来源网址等于 x 时如何显示文本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60534336/