I've got a CSJ variable to capture the last parameter of the URL. Given I'm interested on capturing the location and its position may vary (see example below), I managed to create a custom variable that will always give me the last parameter in the URL.
URL examples:
https://www.example.co.nz/location/**holmwood**
https://www.example.co.nz/find-a-location/auckland/**central-auckland**
The issue I'm having is that my script (see below) is not only capturing the last parameter of the URL, but any string after the "?" symbol, which are mainly UTMs.
Code:
function(){
var pageUrl = window.location.href;
return pageUrl.split("/")[pageUrl.split("/").length - 1];
}
So, on my GA view instead of seeing the ph + the location, I see a large string:
I know I could use page path and remove query from there, but for a specific event I'd rather sort that out from the custom variable because of the type of value I'm passing.
What else should I add to my script to keep it completely the same and exclude any query parameters that might be automatically tagged?
Thanks.
Rather than returning the first split, I would then put it through an additional one where you are splitting on the '?'
function(){
var pageUrl = window.location.href;
var lastSlash = pageUrl.split("/")[pageUrl.split("/").length - 1];
return lastSlash.split("?",1);
}
Related
im not that familir with javascript but i need some help . Basically i have this scenario : i want to get a parameter from url , lets say http://mysite1.com/path/#me=VALUE_OF_ME_PARAMETER here also i have a javascript code which redirects to a second site , lets say mysite2.com/path1 , so what i need is when mysite1 redirects to mysite2 i want it also to return http://mysite2.com/path1/#me=VALUE_OF_ME_PARAMETER , where #me parameter value is the same.
If you wish to reuse the complete "hash" part of the Location (the part after and including #...), you can say:
window.location = "http://mysite2.com/path1/" + window.location.hash;
This will also redirect you to http://mysite2.com/path1/#me=VALUE_OF_ME_PARAMETER, if executed from http://mysite1.com/path/#me=VALUE_OF_ME_PARAMETER.
Check the following code: First you must get the url via window.location.href method. Then I make a function using regex obj with two arguments. First the url string and second the name of the url parameter that i want to get.
var url_string = "http://localhost:9080/l?state=12345&code=my_code_here";
function getValueUrl(url,name){
var a=new RegExp(name+'=([^&]+)');
a=a.exec(url)[1];
return a;
}
console.log(getValueUrl(url_string, 'code'));
I have some Javascript that parses out the name of a site so that I can query an XML file to pull data where the node's attribute is the last part of a URL.
<script>
function myExampleSite()
{
var myURL = window.location.href;
var dashIndex = myURL.lastIndexOf("-");
var dotIndex = myURL.lastIndexOf(".");
var result = myURL.substring(dashIndex + 1, dotIndex);
return result;
}
var exampleSite = myExampleSite();
</script>
For example, if the site is http://myexamplesite.com/status-Blah00 I would be able to get all data out of the Blah00 XML node and populate various aspects of the site with whatever is in the XML.
This works fine and I am able to use the URL name (Status-Blah00, Status-Blah01, etc.) to query XML against it and populate elements on the page based on the name of the site.
However I ran into problems where a site has a second - in the URL.
For example:
http://myexamplesite.com/status-Blah01-Blah00.htm
It should be parsing the Blah01-Blah00 node of my XML, but instead of just gets the data from Blah00 since it doesn't recognize the first -. I'm new to javascript and I'm confused as to how to basically do:
if 1 "-" in url then get last index
else the number of "-" in url is > 1, get first index.
How would I be able to count the number of "-" in the URL and logically do just that with the above Javascript?
You could use a regex for this problem. Here is a start:
"status-Blah01-Blah00.htm".match(/([^-]+)/g)
That code generates the array:
["status", "Blah01", "Blah00.htm"]
So you can work with the length of that array to find out how many hyphens are in the url.
Even easier: "status-Blah01-Blah00.htm".split('-') returns the same array.
Here is a single line with sequential regexes that can handle dashes occurring elsewhere in the url and that keeps the Blah01-Blah00 node as a single string rather than separating them, as it seems you requested.
"http://www.site-name.com/folder-name/01-10-20/status-Blah0100-Blah01.htm".match(/-([^.\/]+)\.htm/g)[0].match(/[A-z0-9][A-z0-9\-]+/g)[0]
Generates:
"Blah0100-Blah01"
I am writing js function which takes the actual location.pathname + location.seach, so that the user can come back to search result page if he hits back button.
example url: http://127.0.0.1:8000/search_for_book/?titel=&autor=doniyor#
function select_book(bookid){
var backurl = String(window.location.pathname+window.location.search);
//alert(backurl); //<---- this is giving the correct full path
window.location = 'selected/?book_id=' + bookid + '&back=' + backurl;
}
but in the last line, function is appending only till ?titel= and cuts off the rest. the new url is becoming this:
http://127.0.0.1:8000/search_for_book/selected/?book_id=10&back=/search_for_book/?titel=
why is this? i need full location.pathname with full location.search.
any ideas?
but in the last line, function is appending only till ?titel= and cuts off the rest.
No, it doesn’t. Alerting/logging the string value that you’re assigning to window.location would have shown you that. (And btw., window.location.href is the correct way to update the location. location itself is an object, not a property – only the browsers’ error tolerance lets you do it this way. So use window.location.href = … instead.)
It’s going wrong, because & in a URL separates parameters from each other. So the value of your parameter back ends after the &, and then comes a new parameter autor – because you neglected to URL-encode the parameter value properly.
Use encodeURIComponent on the value, before adding it into the string.
I am trying to check if the current url
base_url/index.php?name0=value0&name1=value1&name2=value2...
contains a specific name=value. I tried this
var path = $.inArray('name=value', $(location).attr('href').split('&'));
if (path > -1){ triggers my function...}
But I guess that this wouldn't work if the url is url encoded. Is there a way to check if the url contains name=value without checking all the conditions (split('&') or split('%26')) ?
Split will always work, because & part of url is not encoded if it split parameters. However, you can have name or value encoded in the url. To search for them, you should use encodeURI like that:
var path = $.inArray(encodeURI('name=value'), $(location).attr('href').split('&'));
if (path > -1){ triggers my function...}
You can use core javascript for this:
var parameterName = 'name0';
var parameterValue = 'value0';
var path = decodeURI(location.href).indexOf(parameterName+'='+parameterValue);
if (path > -1){
triggers my function...
}
EDIT: I've tested it more and neither solution is perfect: mine fails when you have something before the specified name value, for example: varname0 when you check name0 will be found and that's not correct, yours (and monshq's) doesn't check the first value/pair which follows ? character.
How can I get query string values? is something you're looking for.
So let's say there's a URL http://example.com/index.html/hello/hi where "hello" and "hi" are parameters.
How would you use javascript and forms method POST to extract the parameters?
Your subject is a little bit vague. However, I thought I'd made an example of possibilities.
http://jsfiddle.net/tive/LjbPq/
The idea is to split the URL for each character /, in whatever way you received it.
var parts = document.URL.split("/");
Since split() returns an array (zero based), you need to distract 1 from the total length to get the last index.
var lastPart = parts[parts.length - 1];
Run this in a for loop, and you should get the idea as occurring in the example.
documentation on document.URL to retreive the complete URL
documentation on window.location to use properties of a url (protocol, href, pathname, ...)
this could work...
var secondvar = window.location.href.split('/')[window.location.href.split('/').length];
var firstvar = window.location.href.split('/')[window.location.href.split('/').length-1];