PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » JavaScript » Current Page with JavaScript

Current Page with JavaScript

This is like a replacement for PHP's SCRIPT_NAME with JavaScript.

location.href.split('/').pop();

For example with this URL:

http://css-tricks.com/examples/ScriptName/index.php

This code:

document.write( location.href.split('/').pop() );

Would write to the page: "index.php"

Reference URL

Subscribe to The Thread

  1. There’s a significant problem with this snippet:

    If you change the example page to:
    http://css-tricks.com/examples/ScriptName/

    You will see that NOTHING is returned. So if you’re writing code that relies on ‘index.php’ being returned, your code will not execute.

    • That’s right. It would only be really useful if you were running a web app that enforced URLs in a strict way so there wasn’t any possibility of multiple URL’s going to the same place.

    • Abhisek

      Any workaround?

  2. Stuart

    Chris is there away to have more than 2 scripts on a page .I had a Accordion rotation, and a drop-down
    Apple style Navigation bar both worked
    but i didn’t have any page scroll bar so i couldn’t look at the content. I have been learning this for about a week
    so i am not great at JavaScript. So how would i go about it? I make most of my sites in Dreamweaver
    so after my mock up i save as a template.
    Is it still possible to put more scripts on still.
    is there a hack ? Not sure what to do .Thanks

  3. It will aslo include request arguments and the hash like

    inde.php?s=products#data-tab

    art

  4. where is the tutorial???

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~