2017-03-09 24 views
1

如果頁面有一個特定的URL,是否有一個運行腳本的jQuery事件處理程序?類似於$(document).ready(function() {,但僅當頁面具有特定URL時。基於URL運行jQuery的事件處理程序

我想在頁面準備就緒時運行腳本,但只適用於URL爲'.../something/somethingelse /'的頁面。

的觀點具有重定向到新的網址,以改變佈局方面的聯繫 - 但它同樣的觀點(我使用Django的Web框架)

+2

檢出window.location –

回答

1

您可以使用window.location象下面這樣: -

一個例子: -

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

注: - 你可以檢查console.log(window.location);看看有什麼可用的選項,你可以使用。謝謝

+0

這很好。謝謝! –

+0

@RuthYoung很樂意幫助你。:) :) –