Get a parameter from a rewritten URL with javascript - javascript

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];
}

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];

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

Related

How to access REAL querystring param in javascript with URL rewrite in place

I am trying to alert the querystring param in jquery because i need that ID on a page load so that i can create some html elements when the control is loaded. I cant seem to access the parameter as the URL rewrite in place and URL looks diff then the original one.
Original URL
www.somesite.com/camping?ProductId=2457&ism=0&cl=PURPLE
URL seen in browser
www.somesite.com/camping/travel-mug-16oz-double-wat-with-adapter-p2457.aspx?ism=0&cl=PURPLE
now, how do i access the ProductId querystring value in Jquery.
Jquery
$(document).ready(function () {
var param = getParameterByName("ProductId");
alert(param) // shows PURPLE as cl in URL is set to cl=PURPLE
});
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
EDIT:
here is the screenshot when i am trying to access the Querysting object on serverside..
Your current code is attempting to access the value from the querystring, which no longer exists as it has been rewritten on the server.
You can use a regular expression to dissect the rewritten URL, no jQuery required. Assuming your productId is always prefaced by -p and is immediately followed by .aspx, then this will work:
var url = 'www.somesite.com/camping/travel-mug-16oz-double-wat-with-adapter-p2457.aspx?ism=0&cl=PURPLE'
var matches = url.match(/-p(\d+)\.aspx/);
var productId = matches && matches[1];
console.log(productId); // = 2457
productId will be null if there are no matches to the regex.
Example fiddle

Getting a number from within a URL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get query string values?
I am trying to get a page number from a URL during a test. The URL looks like this:
http://www.site.com/objects/search/8765XX/results?page=2&sort_att=posted_dt&sort_dir=desc
What I want to get is the page number right after 'page=' in the url.
In one line, by splitting url. jsfiddle
var url = "http://www.site.com/objects/search/8765XX/results?page=2&sort_att=posted_dt&sort_dir=desc";
var pageNumber = parseInt(url.split("page=")[1].split("&")[0], 10);
A simple pure JavaScript implementation I can think of would be something like the following:
var url = http://www.site.com/objects/search/8765XX/results?page=2&sort_att=posted_dt&sort_dir=desc
var matchedPos = url.search("page=\\d");
var matched = url.substr(matchedPos);
var num = matched.split("=")[1]; //might need to parse
I have this small function to fetch URL parameters (I found it on internet many years ago) :
function getUrlParameter(name, defaultValue) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return defaultValue;
else return results[1];
}
I use it like this :
pageNumber = parseInt(getUrlParameter('page'), 10);

How do I extract "search term" from URLs?

How do I extract "test" from the following URL?
http://www.example.com/index.php?q=test&=Go
I've found the a script to extract the path (window.location.pathname), but I can't find or figure out how to extract or split the URL.
-ben
var m = window.location.search.match(/q=([^&]*)/);
if (m) {
alert(m[1]); // => alerts "test"
}
var myURL = 'http://www.example.com/index.php?q=test&=Go';
function gup( name ) //stands for get url param
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( myURL );
if( results == null )
return "";
else
return results[1];
}
var my_param = gup( 'q' );
Here is the jsfiddle
Or you can use jQuery's plugin:
URL Parser JQuery
If you just want the value of the first term, then:
function getFirstSeachValue() {
var s = window.location.search.split('&');
return s[0].split('=')[1];
}
If you want the value of the 'q' term, regardless of where it is in the search string, then the following returns the value of the passed term or null if the term isn't in the search string:
function getSearchValue(value) {
var a = window.location.search.replace(/^\?/,'').split('&');
var re = new RegExp('^' + value + '=');
var i = a.length;
while (i--) {
if (re.test(a[i])) return a[i].split('=')[1];
}
return null;
}
Both are just examples of course and should test results along the way to prevent unexpected errors.
--
Rob

How can I get URL parameter in a jquery ready function?

I have the following:
$(document).ready(function() {
window.location.href='http://target.SchoolID/set?text=';
});
So if someone comes to a page with the above mentioned code using a url like:
Somepage.php?id=abc123
I want the text variable in the ready function to read: abc123
Any ideas?
you don't need jQuery. you can do this with plain JS
function getParameterByName( name ) //courtesy Artem
{
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 decodeURIComponent(results[1].replace(/\+/g, " "));
}
and then you can just get the querystring value like this:
alert(getParameterByName('text'));
also look here for other ways and plugins: How can I get query string values in JavaScript?
check out the answer to this questions:
How can I get query string values in JavaScript?
that'll give you the value of the id parameter from the query string.
then you can just do something like:
$(document).ready(function() {
var theId = getParameterByName( id)
var newPath = 'http://target.SchoolID/set?text=' + theId
window.location.href=newPath;
});
To help people coming after me that want to have multiple variables and to enhance Moin's answer here is the code for multiple variables:
function getParameterByName( name ) //courtesy Artem
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
{
if ((results[1].indexOf('?'))>0)
return decodeURIComponent(results[1].substring(0,results[1].indexOf('?')).replace(/\+/g, " "));
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
}

url parameter to save is in JavaScript

I would like to insert a parameter value found the url into my javascript code.. So basically i have the following url www.rene-zamm.com/mp-dthanks.asp?gglid=123123123
Now i have the following javascript code in the same page and i want that number in the url to be visible also in the javascript code below:
var gwoTracker=_gat._getTracker("UA-1639156-3");
gwoTracker._trackPageview("/**NUMBER OF URL HERE**/goal");
Can someone help me please?
You need to parse the querystring. Javascript has no inbuilt way of doing this easily, but there are some nice functions you can use:
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];
}
http://www.netlobo.com/url_query_string_javascript.html
Then in your example you could do:
var queryData = gup("gglid");
var gwoTracker=_gat._getTracker("UA-1639156-3"); gwoTracker._trackPageview("/" + queryData + "/goal");

Categories

Resources