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.
Related
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);
}
I am using C# and I have a CMS that uses an open text field. I am doing a redirect manually appending a query string BUT on top of that, I have editors putting UTMs into the URL. I am trying to track the redirects/vanity URLs so we an see the success of them, but the editors are adding UTMs and when I am transferring the redirect, I prepend "?ref=" to their URL to the second URL.
I need to know how to replace any subsequent question marks in the query string.
The CMS is seeing the second question mark and automatically redirecting to the homepage, because I think it is trying to be smart with the URL and the second question mark is causing it to think the URL is invalid.
So the original URL I am getting looks something like this:
www.mysite.com/somepage?utm_source=foo&utm_medium=bar
BUT it then redirects so I can track the URL and it looks now like this
www.myothersite.com/this-other-page?ref=www.mysite.com/somepage?utm_source=foo&utm_medium=bar
So what I want to do is in the second URL is to replace the second question mark with an ampersand. How would I do only the second or subsequent ones without getting rid of the first one?
I am using Javascript to do the redirect in the view.
My code so far
#{
var currentPageUrl = HttpContext.Current.Request.Url.AbsoluteUri;
}
<script type="text/javascript">
setTimeout(function () {
window.location.href = '#Model.Content.GetPropertyValue("externalRedirectURL")?ref=#currentPageUrl';
}, 200); //will call the function after 2 secs
</script>
I found my answer now using HTML.Raw and Json.Ecode.
When I did generic .Replace in the actual Javascript built string, for some reason the .Replace("?", "&") was adding another ampersand to anything in the query string with an ampersand already in the query string. Not what I expected.
Anyways, here is my answer
<script>
var currentP = #Html.Raw(Json.Encode(#currentPageUrl.Replace("?","&")));
setTimeout(function () {
window.location.href = '#Model.Content.GetPropertyValue("externalRedirectURL")' + '?ref=' + currentP;
}, 200); //will call the function after 2 secs
</script>
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 am trying to do a call to url
http://example.com/controller/action?var1=CD14+%20MDM%20in%20GM-CSF%203%20days%20then%203%20days%20IFN-%CE%B3&var2=56&var3=ENSG00000115415
If you look at var1 that I am passing value 'CD14+ MDM in GM-CSF 3 days then 3days IFN-γ'.
When in the controller I try to get this var 1 using request.params.get("var1"), i get 'CD14 MDM in GM-CSF 3 days then 3days IFN-γ' with missing + and instead getting extra space after CD14
How can I pass + in my variable in url
This has an answer here. It says that + needs to be encoded as %2B.
If you are intended to use plus as a data component, instead of url component, You should pass %2B instead.
Javascript :
var url = "http://example.com/controller/action?var1=CD14+%20MDM%20in%20GM-CSF%203%20days%20then%203%20days%20IFN-%CE%B3&var2=56&var3=ENSG00000115415";
//NOTE: this following wil lreplace all + to %2B
url = url.replace(/\+/g,"%2B");
console.log("new : ",url);
Try '%2B' (as other have stated).
But if you are in Javascript, you could just use the built-in method for doing URL encoding called "encodeURIComponent". Note you need that and not "encodeURI" which will not encode reserved characters like '+'. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
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];