PDA

View Full Version : Getting the page that called a script


Verbetex
03-29-2003, 11:29 AM
I have a JavaScript file with all my scripts in.

What I need to do is find which page the code has been called from and put that page name into a variable to do other stuff with.

How do I do this??

liorean
03-29-2003, 12:23 PM
document.title will contain the title of the page. document.URI will contain the address, as will location.href.

cheesebagpipe
03-29-2003, 11:30 PM
Think that's document.URL...

http://www.dardenhome.com/js/x275094.htm

You could also simply embed a variable in the HTML file itself, identifying the page. Everything gets loaded into the same programming environment, regardless of its source.

http://www.augustana.ab.ca/~mohrj/courses/2000.fall/csc110/JavaScript/Guide/getstart.htm#1009589

joh6nn
03-30-2003, 12:31 AM
last i checked it was document.URL, (though that will probably change) and i agree with liorean that using document.URL or window.location.href would the best ways to do it; hardcoding a variable into each page would be 1) a pain, and 2) reinventing the wheel.

there's an important difference between document.URL and window.location.href, though. window.location.href returns the url that was requested, and document.URL returns the url of the page that was loaded. these aren't necessarily the same; an example of that would be with a 404 page. window.location.href might return whatIwant.html, but document.URL would return 404.html. that means, that for your purposes, you probably want to use document.URL

liorean
03-30-2003, 12:58 AM
Yep, it is document.URL. Be aware that document.URL only exists on documents parsed as html, though. The core DOM doesn't have it, and thus xml documents doesn't either.