I use document.URL to detect if a user is on index.html:
if(document.URL.indexOf("index") >-1) return true;
But if the user types "mydomain" or "mydomain/" then the test returns false.
I could try:
if(document.URL ==="") return true;
But I want to use this code on different domains. Any suggestions?
I use document.URL to detect if a user is on index.html:
if(document.URL.indexOf("index") >-1) return true;
But if the user types "mydomain.com" or "mydomain.com/" then the test returns false.
I could try:
if(document.URL ==="http://myDomain.com") return true;
But I want to use this code on different domains. Any suggestions?
Share Improve this question edited Jan 24, 2012 at 15:56 Ben Everard 13.8k14 gold badges68 silver badges96 bronze badges asked Jan 24, 2012 at 15:53 Chris TolworthyChris Tolworthy 2,0964 gold badges20 silver badges24 bronze badges 3- 1 does this have to be done with javascript? can it be done with PHP? – bowlerae Commented Jan 24, 2012 at 15:59
- Wouldn't PHP have to be online? The page is a Javascript game, and I want the users to have the option to save it onto disk. So if page detection is all Javascript that's a bonus. – Chris Tolworthy Commented Jan 24, 2012 at 16:07
- 1 ok, I think that's an example of you needing to provide more info :-P – bowlerae Commented Jan 24, 2012 at 16:12
4 Answers
Reset to default 11There are so many permutations of URL that could mean that a user is on index.html
. Instead could you not put a var within that file:
<script type="text/javascript">
on_index = true;
</script>
Just check if on_index
is not undefined and is true. That'll be accurate 100% of the time.
javascript Location object has many useful properties, in particular, you can examine location.pathname
.
Basically, you're on the "index" page if the pathname is 1) empty 2) is equal to a slash /
3) starts with index
or /index
.
var p = window.location.pathname;
if (p.length === 0 || p === "/" || p.match(/^\/?index/))
alert ("on the index page!")
See Javascript .pathname IE quirk? for the discussion of leading slash issues.
There isn't a direct link between files and URLs. Additionally, index.html
does not need to be in the site's root and the default page does not need to be index.html
.
If you want a generic solution, you're probably out of luck. If you want a solution for your particular case, you can just provide that info from the page itself, e.g. defining an ID or class name:
<body class="index">
... or a JavaScript variable:
// Pick one
var page = 'index';
var isIndex = true;
If all you want is some simple string manipulation with current location, grab the pathname
property of the window.location
object:
// Untested
if( window.location.pathname=="/" || window.location.pathname=="/index.html" ){
}
You could use
if (document.location.pathname === '/' ||
document.location.pathname.indexOf('index') >-1 ) {
return true;
}
If you have access to the actual page and not just the script then you should follow @Ben Everard's advice.
Just make sure you include the snippet he proposes before your script..