根据URL运行基于jQuery的事件处理程序

3
有没有一个jQuery事件处理程序,如果页面具有特定的URL,则运行脚本?类似于$( document ).ready(function() {,但仅在页面具有特定URL时才运行。我想在页面准备就绪时运行脚本,但仅适用于URL为'.../something/somethingelse/'的页面。该视图具有链接,可重定向到新的URL以更改布局方面,但它是相同的视图(我正在使用django web框架)。

2
请检查 window.location。 - enno.void
1个回答

3
您可以像下面这样使用window.location
一个例子:

if(window.location.href =='http://stacksnippets.net/js'){
  console.log(window.location.hostname);
  console.log(window.location.href);
}else{
  $('#getHostNameandHref').html(window.location.hostname +'----'+window.location.href);
  console.log('other page');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="getHostNameandHref"><div>

注意:您可以检查console.log(window.location);以查看可用选项,您可以使用这些选项。谢谢。


@RuthYoung 很高兴能帮助你。:):) - Alive to die - Anant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接