Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the current filename (revisited)...

Status
Not open for further replies.

Albion

IS-IT--Management
Aug 8, 2000
517
US
I'm trying to get the filename of the current page. I've tried using document.location.href and it works only part of the time. It seems that the object only contains what is in the address bar. For instance and both display index.html, but only the second will supply document.location.href the actual path AND filename.

Is there a way to get the current file name whether it's in the address bar or not?
 
Hi

Albion said:
Is there a way to get the current file name whether it's in the address bar or not?
Only the web server knows what is actually served.

Your only solution is to put the server or the server-side script which generated the document to set a corresponding JavaScript variable. But if your index file is a static HTML file, then there is no way.

But why not just add 'index.html' yourself if [tt]location.href[/tt] not contains it ?

Feherke.
 
That's exactly what I did.

I added:

Code:
<div id="idCurrentFilename" style="display:none">index.html</div>

to the <body> of index.html, and then to my script:

Code:
if(document.getElementById('idCurrentFilename') != null) {
    var fileName = document.getElementById('idCurrentFilename').innerHTML;
} else {
    var filename = "";
}

Works perfectly.
 
personally i'd use a hidden input field for storing client side variable data.

Rather than hiding something with CSS that might not work if CSS is disabled and could be seen as stealth by the search engines!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
@1DMF: That's actually a better idea. Thanks for bringing that to my attention.

@Diancecht: I have some jQuery code that works on multiple pages. I want it to run differently if the user is on the index.html page. Instead of creating the same code twice increasing the size, I figured I could add a conditional depending on the page that the user was looking at. That would decrease the amount of code a user downloads by at least 50 lines in my situation.

-Al
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top