5

I am looking to append a var to a url with a function that i have bounf to a click event.

Say the url is www.example.com

after running the function it becomes.

www.example.com?folder=beats

I have got the function grabbing the right folder value but need to append it to the current page url.

function fileDialogStart() {

$folder = $('#ams3Folder').val();



}

any help

2
  • 1
    The browsers URL or just some string? Commented Jul 4, 2011 at 16:08
  • 1
    use var to declare variables so you do not create an implied global. Commented Jul 4, 2011 at 16:10

2 Answers 2

10
function fileDialogStart() 
{
    var newURLString = window.location.href + 
            "?folder=" + $('#ams3Folder').val();

    window.location.href = newURLString;    // The page will redirect instantly 
                                            // after this assignment
}

For more information, go here.

Sign up to request clarification or add additional context in comments.

1 Comment

David Walsh has a great page on window.location too: davidwalsh.name/javascript-window-location
3
function fileDialogStart() {

$folder = $('#ams3Folder').val();

window.location.href = window.location.href + $folder

// window.location.href = window.location.href + '?test=10';

}

2 Comments

hi tarek just one problem is if i run it twice i get www.example.com?folder=beats?folder=beats which messes it up????
you have 2 options : clean up the location.href using replace() before appending variable or do something like : window.location.href = 'index.php?'+$folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.