optikalefx Posted January 28, 2008 Share Posted January 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
glenelkins Posted January 28, 2008 Share Posted January 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 28, 2008 Share Posted January 28, 2008 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> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 28, 2008 Share Posted January 28, 2008 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> Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 29, 2008 Author Share Posted January 29, 2008 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? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 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> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 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> Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 29, 2008 Author Share Posted January 29, 2008 it looked so good when i looked it over but i tested it on 2 index pages and both show up blank. the url is http://www.site.com and i tried http://www.site.com/folder both load index.php but index.php doesnt show up in the script Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 try changing this: if (total > "2") to this: if (total > "3") Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 29, 2008 Author Share Posted January 29, 2008 still blank. did it work when you tried it? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 ok - try this; I had to change a few things, sorry made a mistake or two. <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> Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 29, 2008 Author Share Posted January 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 if you have your htacess file set where it changes your extensions in sub-directory urls; then it's not going to work. example: before htacess change http://www.domain.com/whatever.php after htacess change http://www.domain.com/whatever/ Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 29, 2008 Author Share Posted January 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 29, 2008 Author Share Posted January 29, 2008 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 Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 29, 2008 Author Share Posted January 29, 2008 This is the working one This is not Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 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 Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 try this; I thought I would give this one more shot <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> Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 30, 2008 Author Share Posted January 30, 2008 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, Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 30, 2008 Author Share Posted January 30, 2008 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! Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 yeah, I found a bug in the script; I am taking a look at it now. Quote Link to comment Share on other sites More sharing options...
optikalefx Posted January 30, 2008 Author Share Posted January 30, 2008 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.