我正在使用在线商店解决方案,这允许我建立一个简单的在线商店。我不能使用除html / css / javascript之外的任何代码。
我在模板中放入了一个简单的图像滑块。但我希望这个滑块只显示在首页上。现在它显示在每一页上。
我可以使用类似这样的javascript函数:"如果url是" www.example.com"然后显示图像滑块,否则隐藏它。"
这样的事可能吗?
<script>
$(function() {
if(document.URL == "http://example.com/") {
...
...
</script>
事先感谢:)
答案 0 :(得分:1)
我不知道您尝试做的事情的确切情况或您为何需要它,但
if (location.href == "http://example.com")
应该这样做。 Location.href返回页面的URL,例如&#34; document.URL&#34;在你的例子中。
如果您希望获得网址的某些部分,我发现这是一个非常酷的提示here。
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.hostname; // => "example.com"
parser.pathname; // => "/pathname/"
基本上它的作用是在JavaScript中创建一个链接元素,该元素具有返回URL不同部分的属性。如果您的索引页面可能有多个URL,那么这将是相关的。例如,如果用户当前位于http://example.com/index#something。
(location.href == "http://example.com/")
会返回false。但是,如果您在代码中执行此操作,
var parser = document.createElement('a');
parser.href = "http://example.com/index#something";
(parser.hostname+parser.pathname == "example.com/index")
对于http://example.com/index和http://example.com/index#something,最后一行都会返回true。 根据您提供的有关网站的信息,我最好猜测您的代码应该是什么样的。
var parser = document.createElement('a');
parser.href = location.href;
if (parser.hostname+parser.pathname != "example.com/index") //If user isn't on the index page
{
$(".slidewrap").hide(); //Hide the div with the class slidewrap
}
答案 1 :(得分:1)
window.location
是正确的区域,它会公开hostname
属性,因此您只能检查网站名称而不是整个网址,而pathname
仅显示网站中的本地路径。见https://developer.mozilla.org/en-US/docs/Web/API/Location
因此,如果主页为http://www.example.com/,则为window.locaton.pathname === '/'
即
<script>
$(function() {
if (location.pathname == "/") {
...
}
});
</script>
答案 2 :(得分:1)
我只是添加解决方案作为答案,因为我通过混合j4g和duncans代码来实现它:
<script>
$(function() {
if(location.pathname !== "/") {
$("#slidewrap").hide();
}
});
</script>
据我了解。它说:如果位置不是索引,那么隐藏#slidewrap:D并且完美无缺。感谢。