使用 javascript 根据推荐人 URL 更改 IMG SRC
Change IMG SRC based on referrer URL using javascript
我不是最擅长这方面的,但我想知道是否有一种方法可以使用 javascript 根据引用 URL 更改图像 src。我把它拼凑在一起,但它不起作用,我不确定为什么。谢谢
<script>
var ref = document.refferer;
if(ref.includes("dev.mastercoolproducts.com")){
$(document).ready(function() {
$("#fc-logo").attr("src","https://essickair.foxycart.com/cache?url=https://www.dropboxusercontent.com/s/uhy2eoxjojeqwsj/mc-logo.png?dl=0");
});
}else{
$(document).ready(function() {
$("#fc-logo").attr("src","https://essickair.foxycart.com/cache?url=https://dl.dropboxusercontent.com/s/q635dlso4f8pdol/ac-color.png?dl=0");
});
}
</script>
看来您可能 运行在文档初始化之前就已经使用了这段代码。您只想 运行 代码来检查您的文档是否被初始化一次。我修改了您的代码,认为这应该可以解决问题:
//Only run once the document is ready
$(document).ready(function() {
// this sets the value to '' if refferrer is somehow empty
var ref = document.refferer || '';
// since you'll be searching for this node multiple times, just store it in a variable:
var logoImg = $("#fc-logo");
if (ref.includes("dev.mastercoolproducts.com")) {
logoImg.attr("src", "https://essickair.foxycart.com/cache?url=https://www.dropboxusercontent.com/s/uhy2eoxjojeqwsj/mc-logo.png?dl=0");
} else {
logoImg.attr("src", "https://essickair.foxycart.com/cache?url=https://dl.dropboxusercontent.com/s/q635dlso4f8pdol/ac-color.png?dl=0");
}
})
我不是最擅长这方面的,但我想知道是否有一种方法可以使用 javascript 根据引用 URL 更改图像 src。我把它拼凑在一起,但它不起作用,我不确定为什么。谢谢
<script>
var ref = document.refferer;
if(ref.includes("dev.mastercoolproducts.com")){
$(document).ready(function() {
$("#fc-logo").attr("src","https://essickair.foxycart.com/cache?url=https://www.dropboxusercontent.com/s/uhy2eoxjojeqwsj/mc-logo.png?dl=0");
});
}else{
$(document).ready(function() {
$("#fc-logo").attr("src","https://essickair.foxycart.com/cache?url=https://dl.dropboxusercontent.com/s/q635dlso4f8pdol/ac-color.png?dl=0");
});
}
</script>
看来您可能 运行在文档初始化之前就已经使用了这段代码。您只想 运行 代码来检查您的文档是否被初始化一次。我修改了您的代码,认为这应该可以解决问题:
//Only run once the document is ready
$(document).ready(function() {
// this sets the value to '' if refferrer is somehow empty
var ref = document.refferer || '';
// since you'll be searching for this node multiple times, just store it in a variable:
var logoImg = $("#fc-logo");
if (ref.includes("dev.mastercoolproducts.com")) {
logoImg.attr("src", "https://essickair.foxycart.com/cache?url=https://www.dropboxusercontent.com/s/uhy2eoxjojeqwsj/mc-logo.png?dl=0");
} else {
logoImg.attr("src", "https://essickair.foxycart.com/cache?url=https://dl.dropboxusercontent.com/s/q635dlso4f8pdol/ac-color.png?dl=0");
}
})