I need to Pull a file name from a URL

Discussion in 'JavaScript' started by QuestionMan, Jan 5, 2012.

  1. #1
    I have to pull a file name from a URL and I have found a few scripts to do that the problem that I am running into is the solutions I have found are file name specific. I need to pull a file name out of. This is going into a Document Library so after I pull the name from the Url I will display it on a ribbon above the library and I know the script for that. I have just had a heck of a time trying to find a script that can pull the file name from the Url in that specific location even when the name of the file changes. Sorry if it sounds confusing im not the best at Jacascript http://sharepoint/sites/Jarrod/DurangoTest/SitePages/Home.aspx?RootFolder=%2Fsites%2FJarrod%2FDurangoTest%2FShared%20Documents%2FTest1&FolderCTID=0x01200094D5A58A4F099E49BE1A8BA2F7DE9E0D&View={653454F3-1CE4-48C1-967C-5BA6023D349E}'I need the Test1 portion and have found scripts that will do this for the specific name I need something that will pull that location because the File name will be changing, like this. http://sharepoint/sites/Jarrod/DurangoTest/SitePages/Home.aspx?RootFolder=%2Fsites%2FJarrod%2FDurangoTest%2FShared%20Documents%2FTest2&FolderCTID=0x01200094D5A58A4F099E49BE1A8BA2F7DE9E0D&View={653454F3-1CE4-48C1-967C-5BA6023D349E}Now the file name is Test2.
     
    Last edited: Jan 5, 2012
    QuestionMan, Jan 5, 2012 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    I'm not going to write the code for you, but here's what you need to do:

    Start by using the split() function at the '?' to obtain the query string from the URL.

    Use split() again at '&' to break down the query string into the individual parameters.

    Use a for-next loop to examine each of the parameters.
    Use split() on '=' to break each one into parameter name and value. If the parameter name is ''RootFolder", use unescape() on the value to remove the URL encoding. Use lastIndexOf() on the value to find the last '/'. Then use the result with substr() to isolate the file name.

    If you're not familiar with these functions, a quick Google search will pull up documentation. Good luck!
     
    rainborick, Jan 5, 2012 IP
  3. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #3
    If what you need is ALWAYS after the last "2F" before the first "&", just use slice() or substring() along with indexOf() and lastIndexOf().

    Go here (http://www.w3schools.com/jsref/jsref_obj_string.asp) for help, examples, and a sandbox to test your code.
     
    mmerlinn, Jan 7, 2012 IP
  4. QuestionMan

    QuestionMan Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks Guys
     
    QuestionMan, Jan 9, 2012 IP