I can Get base url of my laravel project from blade file in javascript section by using this
var APP_URL = {!! json_encode(url('/')) !!}
But what should i do to get the base url when i am in a helpers.js like file
isValid(token){
const payload = this.payload(token);
if(payload) {
return payload.iss == "http://127.0.0.1:8000/" || "http://127.0.0.1:8000/register" ? true : false
}
return false
}
i want to get base url here rather asssigning the url separately.
Can anyone Help Me?
Use this to get base url in javascript.
var base_url = window.location.origin;
Check it
var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
You Can Use like this:
var baseUrl = window.location.origin;
// "http://google.com"
var host = window.location.host;
// google.com
Related
How can I get the params from the URL path as we do in Django?
For example if the url is domain.dom/userid,
then we need userid as a param in js and redirect to domain.dom.
Is this possible in javascript?
Try this
let url = new URL("https://domain.dom/freddy_user"); // new Url(location.href)
const userid = url.pathname.slice(1);
url.pathname="index.html";
url.searchParams.set("userid",userid)
console.log(url); // location.replace(url)
If you need the current URL and params, this could help you
I'am considering that the userID param is the first param like:
url.com/123
where 123 is the userID
//Get current base URL
let base_url = window.location.origin;
//Get userID or first param
//Change number 3 if your param is in another position
let userId = window.location.href.split("/")[3];
alert(userId);
//Redirect to base URL
window.location.href = base_url;
This can be achieved by redirecting through the .htaccess file and further
var path = location.pathname; var arr = path.split("/"); var name = arr[arr.length-1];
Hence, with this, we can achieve the parameters through the domain path in javascript.
I get too many ways to get the hostname like code below:
window.location.host // you'll get sub.domain.com:8080 or sub.domain.com:80
window.location.hostname // you'll get sub.domain.com
window.location.protocol // you'll get http:
window.location.port // you'll get 8080 or 80
window.location.pathname // you'll get /virtualPath
In my case I want something different. For example:
My QA site name is example.com/testsite/index.html
My PROD site name is example.com/index.html
The problem here using the above methods to get the hostname it is returning me only the hostname like this: example.com
However for QA I need to return example.com/testsite
For PROD i need to return example.com
Is it possible with the single code? Thanks in advance.
To achieve what you require you'll need to check the window.location.hostname, and also the first folder in the window.location.pathname. Something like this:
function getPath() {
var folder = (window.location.pathname.split('/')[0] || '').toLowerCase() == 'testsite' ? '/testsite' : '';
return window.location.hostname + folder;
}
Best method that works for both PROD & QA
var BASE_URL = window.location.href;
BASE_URL = BASE_URL.split("testsite");
if (BASE_URL.length > 1)
{
BASE_URL = BASE_URL[0];
BASE_URL = BASE_URL + 'testsite';
} else{
BASE_URL = window.location.origin;
}
Use window.location.hostname;
Example:
Page URL is http://localhost:2239/Default2.aspx?id=5&name=SatinderSingh
var getCurrentURL =window.location.href; //http://localhost:2239/Default2.aspx?id=5&name=SatinderSingh
var getHostname=window.location.hostname; //localhost
var getPathName=window.location.pathname // Default2.aspx
var getPortNo=window.location.port // 2239
var getQueryString=window.location.search //?id=5&name=SatinderSingh
var getHostname = window.location.hostname; //localhost
var getPathName = window.location.pathname // Default2.aspx
var split_PathName = String(getPathName.split("/"));
var FinalURL = getHostname + "/" + split_PathName[1]
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);
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();
}
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