使用javascript根据引用URL更改IMG SRC



我在这方面不是最擅长的,但我想知道是否有办法使用javascript根据referrer 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");
}
})

相关内容

  • 没有找到相关文章

最新更新