5

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

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

How can I do this? Thanks!

zmanw
  • 169
  • 1
  • 1
  • 12

2 Answers2

10

The javascript would be:

<script type="text/javascript">
    if(window.location.pathname == '/podcast') {
        // script body here
    }
</script>
kingcoyote
  • 1,145
  • 8
  • 21
  • 1
    Thanks! If i wanted to insert in the body a script like would this work? I dont have access to the script itself right? – zmanw Mar 14 '12 at 23:59
  • 1
    If you're using jQuery, you can use some of the code Mike wrote below: $('body').append($('')) – kingcoyote Mar 15 '12 at 00:55
0

I dont know if this helps but on this page if you use 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 then do something like this if your using JQuery

if( window.location['pathname'] === 'desired_path') 
   $('body').append($('<script ... ></script>'))
Mike
  • 364
  • 1
  • 9