Jump to content

For Javascript experts


kharbat

Recommended Posts

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

[code]
<!-- 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>
</tr>
</table>
[/code]


Now the Javascript is

[code]

<script>

    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>";
            
        
        }



</script>
[/code]


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..
Link to comment
Share on other sites

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

[code]                

file_name = new String( File Path );

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

[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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