7

I use the following javascript class to pull variables out of a query string:

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

So this works: http://example.com/signinup.html?opt=login

I need http://www.example.com/login/ to work the same way. Using mod_rewrite:

RewriteRule ^login/? signinup.html?opt=login [QSA]

allows the page to load, the javascript to load, the css to load, but my javascript functions can't find the opt key (i.e., it's undefined). How do I get opt to my javascript?

5 Answers 5

3

Javascript is client-side. Mod_rewrite is server-side.

Therefore Javascript will never see the rewritten URL. As far as your browser is concerned, the URL that you entered is the finished address.

The only real solution is to change your Javascript so it looks at the URL it's got rather than the old version (or possibly parse for both alternatives, since the old URL will still work and people may still have old bookmarks).

The other possible solution would be to go to your server-side code (PHP? whatever?) where you can see the rewritten URL, and insert some javascript code there which you can parse on the client side. Not an ideal solution though. You'd be better of just going with option 1 and changing you Javascript to cope with the URLs it's actually going to be getting.

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

1 Comment

Thanks all! @Spudley's answer is the clearest answer, but everyone gave me great answers. Client/server that makes sense (ooops). Also, others may be interested in @Chris's [R] flag answer.
2

Your issue is that JavaScript runs on the client side, so it will never see the ?opt=login part to which the URL gets converted internally on the server.

Apart from changing your regular expression to match the new URL format, the easiest workaround might be to write a JavaScript statement on server side that introduces the value of the opt variable into JavaScript.

Comments

2

If you're using PHP, you can have the PHP create a JavaScript variable for you. For example:

$params = "?";

foreach($_GET as $key => $value) {
    $params = $params . $key . "=" . $value . "&";
}

echo 'var urlParams = "' . $params . '"';

Now, you JavaScript will have access to a urlParams variable that looks like this

?opt=login&

Then, in your Javascript code, wherever you expected to use the URL parameters, use the urlParams instead.

1 Comment

Thank you very much this is the solution to my problem.
1

If it's a special case, then put it as a special case in some way. If you rewrite generally, change your general regular expression. The way mod_rewrite works, the client never knows the rewritten URL. From the client, it's /login/ and /login/ only. Only the server ever knows that it's really signinup.html?opt=login. So there's no way your regular expression or location.href can know about it.

Comments

1

Unless you use the [R] flag in your RewriteRule, the browser (and thus javascript) will never know about the new URL. If you don't want to be redirecting people, you're going to have to add some code to your login page that GET parameters as javascript in the page.

Comments

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.