3

Is there a jQuery event handler that runs a script if the page has a certain URL? Similar to $( document ).ready(function() { but only if the page has a particular URL.

I want to run a script when the page is ready but only for a page with URL '.../something/somethingelse/'.

The view has links that redirect to new URLs to change layout aspects - but it's the same view (I'm using django web framework)

1
  • 2
    check out window.location Commented Mar 9, 2017 at 9:37

1 Answer 1

3

You can use window.location like below:-

An example:-

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>

Note:- you can check console.log(window.location); to see what available options are there which you can use.Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

@RuthYoung glad to help you.:):)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.