2

I am trying to get a url parameter using javascript so i can pass the paramter to google maps

The problem is i'm using mod rewrite on the url

www.mysite.com/1/my-event

instead of

www.mysite.com/mypage.php?id=1&name=my-event

I've tried doing an alert but it comes up blank

Here is the javascript function that will work if i don't rewrite the url

function gup( name ){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}
1
  • JS works on the client side. The url it has to work with is the URL you see in the address bar. The server-side rewrite with the query parameters is NOT visible to JS, unless the server-side rewrite forces a client redirect to the rewritten url. Commented Nov 10, 2011 at 21:28

2 Answers 2

10

The rewritten format, with the query-string, isn't available to your JavaScript.

You'll have to grab the value out of location.pathname (/1/my-event in your example), instead:

var params = window.location.pathname.split('/').slice(1); // ["1", "my-event"]

var id = params[0];
var name = params[1];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks...i found something similar a minute ago
1

Just split the URL on / characters and take the last elements in the resulting array, mapping them to the names you expect.

1 Comment

Use JS's string split() function.

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.