Jump to content

How to identify the page type on the current page


optikalefx

Recommended Posts

 

glenelkins is right; the best way to do this is server side, but it can be done through javascript. the only draw back is, that you would need to know how many forward slashes were in your url. this can be probably done with a little bit of javascript math. but the basic idea is below - good luck ;)

 

<script language="javascript">
function getFileNameAndExt()
{
var presently = document.URL;
var thisFile = presently.split("/");
document.getElementById('viewIt').innerHTML = thisFile[3] + "<br/><br/>";
}
</script>

<div id="viewIt"></div>

<a href="javascript:getFileNameAndExt()">Where Am I At? - Click Here To Find Out</a>

Link to comment
Share on other sites

here is a better version of my script above. this version will use some math too automatically find the amount of forward slashes, so it will always pull your file name and extension from the url. it shouldn't matter how long the url is; I tested it in up to 5 sub-directories and each time it gave me the filename with the extension. so I hope the version works out better for you. :)

 

<script language="javascript">
function getFileNameAndExt()
{
var presently = document.URL;
var thisFile = presently.split("/");
var total = thisFile.length - 1;
document.getElementById('viewIt').innerHTML = thisFile[total] + "<br/><br/>";
}
</script>

<div id="viewIt"></div>

<a href="javascript:getFileNameAndExt()">Where Am I At? - Click Here To Find Out</a>

Link to comment
Share on other sites

try this:

 

<script language="javascript">
function getFileNameAndExt()
{
var presently = document.URL;
var thisFile = presently.split("/");
var total = thisFile.length - 1;
if (total > "2") {
document.getElementById('viewIt').innerHTML = thisFile[total] + "<br/><br/>";
}
else {
document.getElementById('viewIt').innerHTML='index.html'; // usually the domain is pointed towards the "index" page; but the extension my vary
}
}
</script>

<div id="viewIt"></div>

<a href="javascript:getFileNameAndExt()">Where Am I At? - Click Here To Find Out</a>

Link to comment
Share on other sites

 

here is a workaround for unknown "index" page extensions:

 

<script language="javascript">
var presently = document.URL;
var thisFile = presently.split("/");
var thisExt = presently.split(".");
var total = thisFile.length - 1;
function getFileNameAndExt()
{
if (total > "2") {
document.getElementById('viewIt').innerHTML = thisFile[total] + '<br/><br/>';
}
else {
document.getElementById('viewIt').innerHTML='index.' + thisExt[total] + '<br/><br/>';
}
}
</script>

<div id="viewIt"></div>

<a href="javascript:getFileNameAndExt()">Where Am I At? - Click Here To Find Out</a>

Link to comment
Share on other sites

ok - try this; I had to change a few things, sorry made a mistake or two. :D

 


<script language="javascript">
var presently = document.URL;
var thisFile = presently.split("/");
var thisExt = presently.split(".");
var total = thisFile.length - 1;
var totaled = thisExt.length - 1;
function getFileNameAndExt()
{
if (total > "3") {
document.getElementById('viewIt').innerHTML = thisFile[total] + '<br/><br/>';
}
else if (totaled == "2") {
document.getElementById('viewIt').innerHTML='index.' + thisExt[totaled] + '<br/><br/>';
}
else {
document.getElementById('viewIt').innerHTML='Unknown';
}
}
</script>

<div id="viewIt"></div>

<a href="javascript:getFileNameAndExt()">Where Am I At? - Click Here To Find Out</a>

Link to comment
Share on other sites

yea its not changed.  As far as i know thats basic procedure with webpages.

 

you need either index.htm, index.html, or main.html so that when your site loads www.yoursite.com it knows which page to load, EVEN THOUGH the URL just says http://www.yoursite.com.  Thats the problem that were having, i think javascript just tries to read the URL but the file isnt loaded in the URL its gotta be done some other way.  Which is, im pretty sure what your trying to accomplish here.

 

 

Link to comment
Share on other sites

well im not gonna give out the ftp info to my server. Basically the code you gave me, im pasting it after the <body> tag on my index.htm page.  Upload to ftp then in my browser go to www.mysite.com

 

if you have access to your own site then you can try it, but i cant give mine out

Link to comment
Share on other sites

Also, after looking at you example; if you are not using htacess file to change your urls into sub-directory urls (so to speak) - then even if you type: "http://www.4tenonline.com/createDatabase/" - you should automatically be redirected to: "http://www.4tenonline.com/createDatabase/index.htm". The only way the url will continuously stay in sub-directory format (without showing the file or extension); is if you have changed your htaccess file or if you are using a script or service to mask the url.

Link to comment
Share on other sites

try this; I thought I would give this one more shot :D

 

<script language="javascript">
var presently = document.URL;
var thisFile = presently.split("/");
var thisExt = presently.split(".");
var total = thisFile.length - 1;
var totaled = thisExt.length - 1;
var getEXT = thisExt[totaled];
function getFileNameAndExt()
{
if (total > "3") {
document.getElementById('viewIt').innerHTML = thisFile[total] + '<br/><br/>';
}
else if (getEXT != "htm" || getEXT != "html") // you can keep adding web page extensions as needed
{
document.getElementById('viewIt').innerHTML='index.htm';
}
else {
document.getElementById('viewIt').innerHTML='index.htm';
}
}
</script>

<div id="viewIt"></div>

<a href="javascript:getFileNameAndExt()">Where Am I At? - Click Here To Find Out</a>

Link to comment
Share on other sites

lol thanks for the last attempt, same thing going on though.  So on your server, say you own www.phpquestioner.com.  When users go to www.phpquestioner.com the URL bar says www.phpquestioner.com/index.htm ?  Im not implying anything by this next statement, but i didnt edit my domain forwarding either, i just created a false directory to test this on.  I found that if i dont have an index.htm page in a directory, it actually shows me a dir listing of all the files.

 

The reason i cant be fine with changing the url, is because this needs to work with anybodys domain, not mine, im writing a universal program, so what works only for me, may not for other people.  Thanks again tho,

Link to comment
Share on other sites

 

Try this then:

 

<script language="javascript">
var presently = document.URL;
var thisFile = presently.split("/");
var thisExt = presently.split(".");
var total = thisFile.length - 1;
var totaled = thisExt.length - 1;
var getEXT = thisExt[totaled];
function getFileNameAndExt()
{
if (total > "3") {
document.getElementById('viewIt').innerHTML = thisFile[total] + '<br/><br/>';
}
if (getEXT != "htm")
{
document.getElementById('viewIt').innerHTML='index.htm<br><br>';
}
else {
document.getElementById('viewIt').innerHTML='index.htm<br><br>';
}
}
</script>

<div id="viewIt"></div>

<a href="javascript:getFileNameAndExt()">Where Am I At? - Click Here To Find Out</a>

 

I would like to make this sucker work - lol :D

Link to comment
Share on other sites

OK making progress!!! great job!!

 

The only problem is that if the index file is index.php and not index.htm then it still shows index.htm even though that doesnt exist.  Can we modify to accept all forms of index types?

here are some common ones

 

index.html, index.htm, main.html, main.htm, index.pl, index.php

 

great job btw!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.