Kage
Kage

Reputation: 81

How do I obtain URL variables with Javascript

I'm writing a log-in system using PHP, mySQL and Javascript. My site is effectively a 1 page app written in javascript - only 1 html page. All interaction and navigation is done through javascript.

When a user registers, I create their record in the db with a 32 digit key in the activation column. I e-mail this to the registrant as an activation link. This takes them to a php file that activates their account (or not if there is an error). All well and good.

After activation (or error) I could take them to an html page (e.g. header('somesite.com/success.html') telling them whether their account is activated or not but I'd much rather take them back to a specific function in my 1 page javascript site. How can I do this?

I can take them to the site but how do I pass a message from my php re-direct to the site so it knows whether to display a success or error message?

Do I put it in the URL of the re-direct e.g. http://somesite.com?activation=success? If so, how do I get this variable into my javascript?

I could set a session variable from the php activation script and check it in my code but that seems very clumsy.

I could set a hash in the URL and pick that up in the code but I avoid hash navigation if I can. Any ideas on the method to achieve this?

Final answer from the help below and elsewhere on the site:

function getURLParameter(name) {
return decodeURIComponent(
    (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}

then a redirect on registration such as somesite.com?email=somebody%[email protected]&key=7da93f78cb4942555863c161f50f258d

I can get these variables as simply as getURLParameter('email') and getURLParameter('key')

Thanks for everyone's help. Gotta love this site

Upvotes: 0

Views: 98

Answers (3)

George
George

Reputation: 36794

You can get the variables from the URL with Javascript:

I actually asked a similar question (I can't find it) about getting URL variables with Javascript, and somebody very helpfully gave me this function:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {vars[key] = value});
    return vars;
}

So to obtain a GET variable called 'activation' you would simple call the function like this:

getUrlVars()['activation']

Upvotes: 1

Nick Andriopoulos
Nick Andriopoulos

Reputation: 10643

since you have everything in your one page app, you could use that for the activasion as well -- have the activation link go to http://homesite.com?activation=<32characterkey> and when your app detects the GET param, use AJAX to call the PHP activation, and notify the user of the outcome.

Upvotes: 1

dlock
dlock

Reputation: 9577

You should use to AJAX to login and call your functions in the AJAX success/error callbacks.

You can do that easily with jQuery $.ajax() function

http://api.jquery.com/jQuery.ajax/

Upvotes: 0

Related Questions