Run your script if on a specific web page

I have a script that loads a music player, but I only want to run it on my podcast page.

It should look something like this: if window.location is example.com/podcasts then <script> blah </script>

How can i do this? Thank!

+3


source to share


2 answers


javascript will look like this:



<script type="text/javascript">
    if(window.location.pathname == '/podcast') {
        // script body here
    }
</script>

      

+5


source


I don't know if it helps, but on this page, if you are using window.location, you can do something like the following:

window.location['href']
=> "http://stackoverflow.com/questions/9709337/run-script-if-on-certain-webpage"
window.location['pathname']
=> "/questions/9709337/run-script-if-on-certain-webpage"

      



you can do something like this if you are using JQuery

if( window.location['pathname'] === 'desired_path') 
   $('body').append($('<script ... ></script>'))

      

0


source







All Articles