This is my first post.
Im fairly new to javascript, and was trying to use the following function to grab the document name, (which is mimicked by an element id on the page) to be used in a later function. on the testing site, it works perfectly. for example, if the file is http://testserver/options/example.html it returns ‘example’
one the live site, it always returns www
<script type="text/javascript">
$(document).ready(function() {
var pageName = function() {
//this gets the full url
var url = document.location.href;
//this removes the anchor at the end, if there is one
url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
//this removes the query after the file name, if there is one
url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?")); console.log(url);
//this removes the file extension, if there is one
url = url.substring(0, (url.indexOf(".") == -1) ? url.length : url.indexOf(".")); console.log(url);
//this removes everything before the last slash in the path
url = url.substring(url.lastIndexOf("/") + 1, url.length); console.log(url);
//return
return url; console.log(url);
}
});
</script>
Try modifying this line like so:
Your test site does not have all the “.” like the live site does (i.e. www. or .com). By using lastIndexOf, you can get the last instance of the “.” in the url which will be before the extension.
Change it from:
To:
For more information on lastIndexOf visit this link:
lastIndexOf() on MDN
Hope this helps.