Jump to content


For Javascript experts

  • Please log in to reply
1 reply to this topic

#1 kharbat

  • Members
  • PipPip
  • Member
  • 28 posts

Posted 13 June 2006 - 09:50 AM

Hi there,

In fact i am not really into javascript, but i know some basics. I have a problem with my Compose page in the email system. The main problem is that i need to know if there is a function in javascript that is similar to basename() in PHP that takes a file path and returns the file name only.

another problem.. I am trying to do an attachment section.. the scenario is:

1- Link "Click here to attach a file"

2- When link clicked an HTML upload item appears instead of it

3- When file is selected, the 'File name only' should appear istead of the Upload item. In addition to another 2 links.. "Remove" and "Attach another file"

4- If "Remove" clicked the file name will be removed.

5- If "Attach another file" clicked an HTML upload item appears and so on...

I coud write a simple code, but i didn't know how to continue.. and HOW to display the base name of file only..

<!-- HTML CODE -->

<table border="0" width="100%" id="table5" style="border-collapse: collapse" id="attach_table">
<tr id="r_1">
<td id="c_1_1">
<p style="margin-left: 6px"><font size="2"><a href="#" onClick="Display_Attach(1)">Click here to attach a file</a></font></td>
<td id="c_1_2"> </td>
<td id="c_1_3"> </td>

Now the Javascript is


    function Do_Attach(value)
            file = document.getElementById('file_'+value);
            cell_one = document.getElementById('c_'+value+'_1');
            cell_one.innerHTML = file.value;
            cell_two = document.getElementById('c_'+value+'_2');
            cell_two.innerHTML = "<a href='#' onClick='Remove_Attach("+value+")'>Remove</a>";
            cell_three = document.getElementById('c_'+value+'_3');
            cell_three.innerHTML = "<a href='#' onClick='Display_Attach("+value+1+")' >Attach More</a>";
    function Display_Attach(value)
            cell_one = document.getElementById('c_'+value+'_1');
            cell_one.innerHTML = "<p align='left'><input type='file' name='file_"+value+"' id='file_"+value+"' /></p>";
            cell_two = document.getElementById('c_'+value+'_2');
            cell_two.innerHTML = "<a href='#' onClick='Do_Attach("+value+")'>Attach File</a>";


I appreciate your help with this issue. And by the way i am welling to learn if you prefer to tell me a good keyword for a tutorial to search for. or to give me a link or something..

Thanks and sorry for the long post..

#2 kharbat

  • Members
  • PipPip
  • Member
  • 28 posts

Posted 13 June 2006 - 05:02 PM

Well, i could find the solution myself..

in fact there is no pre-defined function in Javascript that takes a file path, and returns the file name
but you can do it yourself using the following code..


file_name = new String( File Path );

if ( file_name.lastIndexOf("\\") != -1 )
        file_name = file_name.substring ( file_name.lastIndexOf("\\") + 1 , file_name.length );

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users