Jump to content

Archived

This topic is now archived and is closed to further replies.

optikalefx

How to identify the page type on the current page

Recommended Posts

Is there a way in javascript to identify the URL your on?  Not only that but really the file type.  If your on www.aol.com it should return like index.html or something.

Share this post


Link to post
Share on other sites

i think to get the file type from a url like http://www.aol.com/  you would probably be best using server side coding...not client side

Share this post


Link to post
Share on other sites

 

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>

Share this post


Link to post
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>

Share this post


Link to post
Share on other sites

it works great except for one problem

 

if im on www.mysite.com  it doesnt tell me that the page is index.html

it comes up blank.  Any suggestions on this one?

Share this post


Link to post
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>

Share this post


Link to post
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>

Share this post


Link to post
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>

Share this post


Link to post
Share on other sites

I really appreciate you trying to help out.

 

Its still showing up blank.  Ive been trying to figure out whats going on and what i can do to make it work but its just so frustrating, you know.  Thanks again.

Share this post


Link to post
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.

 

 

Share this post


Link to post
Share on other sites

 

yeah, but the else if statement should handle that and I am not quit sure why it is not. do you have a live demo is this; so I can try to replicate this issue? if so, post a link here and let me take a look at it.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

 

well I am not going to change my domain forward page for this either - sorry - lol. I am not sure why it's not working; I suggest just keep toying with the script I provided you or look into a server side method of doing this.

 

good luck ;)

Share this post


Link to post
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.

Share this post


Link to post
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>

Share this post


Link to post
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,

Share this post


Link to post
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

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

wait, i just read what the code is doing, itll say index.htm no matter what.  lol.  This one doesnt actually read from the URL?

Share this post


Link to post
Share on other sites

×
×
  • 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.