[SOLVED] Extracting filename without extension from variable string.

This is a discussion on "[SOLVED] Extracting filename without extension from variable string." within the JavaScript Forum section. This forum, and the thread "[SOLVED] Extracting filename without extension from variable string. are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jan 11th, 2008, 15:20
New Member
Join Date: Jan 2008
Location: Atlanta
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Extracting filename without extension from variable string.

Hi everybody. Here's the deal:

Page Loads, script checks URL string and finds the file name minus ".html" (or whatever the last 5 characters are) then grabs an image from the img folder that has the same name and tosses it into the page. If the file name isn't one of the four listed, a default image is loaded instead.

JavaScript Code:
<div align="left"><font color="#993300">
HTML: Select all
function pickImg() {
zeString=top.document.URL;
zeSlasher=zeString.lastIndexOf("/",zeString.length-1);
zePage=zeString.substring(zeSlasher+1,zeString.length-5);
alert(zePage)
if (zePage=="control", "efficiency", "savings", "solutions") {
document.write("<img src='img/"+zePage+".jpg' alt='My Alt Text' />");
} else {
document.write("<img src='img/total.jpg' alt='My Alt Text' />");
}
}
The above works fine as long as the URL string ends with ".html"
but I have a feeling if the URL ended up being "...contact.html?blahblahblah" my little script would be script-out-of-luck.

How can I make sure I'm always getting the filename minus the extension and any possible passed variables.

Examples are greatly appreciated since I know less than nothing about javascript. Thank you in advance.

Last edited by c010depunkk; Jan 11th, 2008 at 17:02. Reason: please use [HTML] tags when posting HTML
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 11th, 2008, 16:09
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extracting filename without extension from variable string.

Okay, try this.

Code: Select all
function pickImg() {
zeString=top.document.URL;
// First, get rid of the query string after the ?

zeString = zeString.split('?')[0];

//now we'll get the url only after remobing what comes after the last '.'

zePage= zeString.substring(zeString.lastIndexOf('/'), zeString.lastIndexOf('.'));


alert(zePage)
if (zePage=="control", "efficiency", "savings", "solutions") {
document.write("<img src='img/"+zePage+".jpg' alt='My Alt Text' />");
} else {
document.write("<img src='img/total.jpg' alt='My Alt Text' />");
}
}
Bascially doing what you were doing but checking for the last period as well..

hope that helps
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 11th, 2008, 16:36
New Member
Join Date: Jan 2008
Location: Atlanta
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extracting filename without extension from variable string.

Rakuli, thank you very much for your help. I've found another solution for this particular problem, but your advice will come in handy in the future.

FYI, the solution I'm using now:

HTML: Select all
var file = location.href.substring(location.href.lastIndexOf('/')+1);
    var fileName =file.split('.');
    alert(fileName[0]);
...then I run through my comparative if/else statement.


Thanks again.

Last edited by c010depunkk; Jan 11th, 2008 at 17:02. Reason: please use [HTML] tags when posting HTML
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 11th, 2008, 16:54
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extracting filename without extension from variable string.

Cool glad you sorted it.. You have to watch out though becuase if there is more than one period in the url then it will fail with your method.

My method will take away only the extensions.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Random String Verification help psycho wolvesbane PHP Forum 3 Mar 19th, 2008 09:57
[SOLVED] Connection string for password protected databases alexgeek Classic ASP 6 Oct 31st, 2007 15:59
[SOLVED] ASP Session Variable Monie Classic ASP 7 Oct 10th, 2007 02:44
Extracting Text out of XML Nodes DocBoy52 Flash & Multimedia Forum 1 Oct 18th, 2006 14:46
Assign string values to integer values of a session variable Andy K Classic ASP 1 Jul 13th, 2005 08:29


All times are GMT. The time now is 22:56.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42