3

When I visit my url, my script will get the current URL and store it in a variable with

var currentURL = (document.URL);

I'd like to get everything after the forward slash in my url because there will be a hash ID after the forward slash like this:

www.mysite.com/XdAs2 

so this is what would be stored in my variable currentURL and I'd like to take only the XdAs2 from it and store that into another variable. In addition, I'd like to know two other things.

  1. Is document.URL the best way to get the current URL or will I have issues with some browsers?

  2. If I were to try to open that URL using an iframe, will document.URL still work? or must there be an address bar present containing the url? I would really appreciate answers for those questions three questions. Thank you

ivarni
  • 17,658
  • 17
  • 76
  • 92
Brandon
  • 101
  • 3
  • 10

4 Answers4

6

Here's some pseudo code:

var currentURL = (document.URL); // returns http://myplace.com/abcd
var part = currentURL.split("/")[1];
alert(part); // alerts abcd

Basically:

1) document.URL should work fine in all major browsers. For more info refer to this Mozilla Developer Network article or this SO question

2) for an iframe, you need to write something like: document.getElementById("iframe_ID").src.toString()

Community
  • 1
  • 1
Ved
  • 8,577
  • 7
  • 36
  • 69
  • Thank you, what is Pseudo code and can you answer these questions I have too: Is document.URL the best way to get the current URL or will I have issues with some browsers? If I were to try to open that URL using an iframe, will document.URL still work? or must there be an address bar present containing the url? I would really appreciate answers for those questions three questions. Thank you – Brandon Oct 05 '13 at 11:15
  • Updated answer. Please check. – Ved Oct 05 '13 at 11:39
  • Oh sorry, I mean if I open a page in iframe, if the page contains the "Pseudo code" above, will it get the url? for example will the script contained in the page loaded by that iframe be able to get the url from that iframe src so I can then split it and get the hash id after the forward slash XdAs2? – Brandon Oct 05 '13 at 11:46
  • Nope, it will not get URL in that case. – Ved Oct 05 '13 at 12:27
  • Do you know a way to get the URL in that case? – Brandon Oct 05 '13 at 13:01
1

Using jquery, you can do he following in order to access every inch of the current URL: www.mysite.com/XdAs2?x=123

assuming you have the following url: 1- get the url in a jQuery object

var currentUrl = $(location)

2- access everything using the following syntax

var result = currentUrl.attr('YOUR_DESIRED_PROPERTY');

some common properties:

  • hostname => www.mysite.com
  • pathname => XdAs2
  • search => ?x=123

I hope this may help.

Mohammed Swillam
  • 9,119
  • 4
  • 36
  • 47
  • Thank you Mohammed, If I were to try to open that URL using an iframe, will document.URL still work? or must there be an address bar present containing the url? – Brandon Oct 05 '13 at 11:39
  • If I add a username to that url www.mysite.com/Brandon/XdAs2 can I using pathname will I get Brandon and XdAs22 ? – Brandon Oct 05 '13 at 12:17
1

If you want everything after the host, use window.location.pathname

Sandwich
  • 2,304
  • 3
  • 24
  • 30
0

Following on from Mohammed's answer you can do the same thing in vanilla javascript:

var urlPath = location.pathname,
    urlHost = location.hostname;
Antfish
  • 1,313
  • 2
  • 22
  • 41