Passing variable in window.location in Java script is not working - javascript

I am trying to pass a variable in java script but it's not working:
window.location.href = "calulator?dist=" + calculate;
output is like :
http://localhost/jspractice/calulator?dist=%20%20%20%20%20%20%20%205*%20%20%20%205

You just have to decode the received URL with decodeURIComponent()
See example :
var URL = "%20%20%20%20%20%20%20%205*%20%20%20%205";
var decodedURL = decodeURIComponent(URL);
console.log(decodedURL);

Related

How to get return from value from a google script url in javascript

The code below is my google script code. When I simply paste the URL on the browser it is returning a value. but when I try to get the value using javascript. it returns null
Google script
function doGet(e) {
var array = SpreadsheetApp.openById('1qwSU2YVuGea3Dg06yUpUkyhV1Hn-Qf3YshgZkEnJoBo')
.getSheetByName('Sheet1').getRange('A1:A1').getValues();
return ContentService.createTextOutput(array[0]);
}
Javascript
var url_string = "https://script.googleusercontent.com/macros/echo?user_content_key=1AxKHAUiqHNfdbnLALqbmxIoGLQuLR7qjSR7NxCDxgyqacFzuIovzOSyRyu0xkwpe7TF7qJcKknkuPYhV_apY6L9ikxPUB7em5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnMy8zAZf_PeEgqhQVTSu0Ty9Drtp1yaLkXH9-vnTbcP-gooKnCg3eBY8k9BT1tmvR4PZc_4F8_fJkoGWxUlugKqNYjOEVPmtwA&lib=M9OCNb7KeZyOBiWrcHIwQiTdKF1_8u-qf";
var url = new URL(url_string);
var c = url.searchParams.get("");
var c = url.
console.log(c);
alert(c);
Access it with UrlFetchApp.fetch() using method = "Get" and then use response.getContentText() to get the return.

How get root url path

I'm searching a way to get the root URL of my Web project; as example:
Local:
http://localhost:52390/pages/user.aspx
Expected result: http://localhost:52390
IIS:
http://lst.pg.com/iLearn/pages/user.aspx
Expected result: http://lst.pg.com/iLearn
Exists a way to achieve this in ASPX? Or in Javascript/jQuery?
in javascript you can get url information from the location object.
var href = location.href; //returns the entire url
var host = location.hostname; //returns just the hostname of the url.
ASPX: Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
Implemented with Javascript:
<script type="text/javascript">
var rootUrl = '<% =(Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath) %>';
</script>
There are times I need to get the rootpath from the code behind. Here is what I use.
public static string GetRootPath()
{
HttpContext CTX;
CTX = HttpContext.Current;
return CTX.Request.ApplicationPath.ToString();
}

Use URL based on razor method

I want to redirect to particular page.
For that I am using some Javascript function in MVC project as::
function rootUrl(url) {
var _rootUrl = '#Url.Content("~")';
var x = url;
if (url.indexOf(_rootUrl) != 0) {
x = _rootUrl + "/" + url;
x = x.replace(/\/\//g, "/").replace(/\/\//g, "/");
}
return x;
};
which is being used as ::
var url = rootUrl("Home/Company/") + $(this).val();
window.location.href = url;
But I am getting wrong URL in my browser as::
http://localhost:60294/Home/Company/#Url.Content(%22~%22)/Home/Company/7
Why not use Url.Action() directly which gives you url relative to root directory, instead of creating a javascript messy function:
var url = '#Url.Action("Company","Home")' + $(this).val();
Here,Home is the name of Controller and Company is the action of it
You can't access razor in Js file. When I need the urls from Razor in Js I just define them in the view, like:
<script>
var _rootUrl = '#Url.Content("~")';
</script>
This will work

How do I pass a url as string in url string in javascript?

I want link to send as parameter as query string, but the controller does not accept it, what's wrong?
function sendLink(){
var link="http://xyz/1/1"
var url="/abc/"+link;
$.get(url,function(data){
alert(data);
});
}
Use encodeURIComponent():
function sendLink() {
var link = "http://xyz/1/1";
var url = "/abc/" + encodeURIComponent(link);
$.get(url, function(data){
alert(data);
});
}
Your best bet is probably to put it on the query string:
var url="/abc/?pathname="+encodeURIComponent(pathname);
Then access it in your controller as the HTTP GET variable pathname.

Why does this function alert the URL but none of it's string segments?

Why does this code alert the url but neither of the other two alerts after I split/slice the string. As far as I know, either of these methods should split the url as I would but neither are working for some reason.
window.onload = function getPhpExt() {
var url = window.location;
alert(url);
var getinfo = url.split("?");
alert(getinfo[1]);
var n=url.indexOf("?");
var getinfo2 = url.slice(n);
alert(getinfo2);
}
Because window.location is an object and not a string so split is returning an error.
You could use window.location.href instead
Just use window.location.search directly.
alert( window.location.search.substr(1) );
BTW you don't have to wait for onload.

Categories