This question already has answers here:
Get absolute path of file on content
(5 answers)
Closed 6 years ago.
I'm trying to resolve the root url for the application in javascript using the following code:
var rootUrl = '#Url.Content("~")';
But the above code gives rootUrl as /.
What should I do to get the url as http://localhost:8000 on which my application is running.
You can get It directly from JavaScript:
var rootUrl = window.location.href;
alert(rootUrl);
The location property points to an object that contains information
about the URL of the currently loaded page.
You will get the same results with: window.location, location, location.href
Read more about window.location here
I also needed something similar a while back. My solution might not be the correct way of doing it but it was all that could find at the time. It worked for me and can work for you as well.
var rootUrl = "#Url.Content("~")";
Using the above code will give you this result:
var rootUrl = "/";
For what you are looking for you need to change your code to this:
var rootUrl = "#(new Uri(Request.Url, Url.Content("~")))";
Using the code above will give you the following result:
var rootUrl = "http://localhost:8000/";
I hope this helps.
#Url.Content("~/") is used to get your current application folder.
#Request.Url.Authority is used to get current host (with port)
So in able to get what you want you may want to mix them:
#String.Format("{0}://{1}{2}",Request.Url.Scheme, Request.Url.Authority,Url.Content("~/"))
Hope this helps!
Related
How can I retreive a domain name of the current page with javascript, and add it to url parameter like this before redirecting:
http://redirecturl.com?domain={domain}
Quick and easy answer below. However, I little research would have yielded the same without stackoverflow
let url = `http://redirecturl.com?domain=${document.location.hostname}`;
console.log( url );
You can use a simple location.hostname:
const redirectURL = `http://redirecturl.com?domain=${location.hostname}`;
console.log(redirectURL);
ES5:
var redirectURL = "http://redirecturl.com?domain=" + location.hostname;
console.log(redirectURL);
Note: The above return stacksnippets.net because that's where StackOverflow run their snippets (as you will see if you visit the website
This question already has answers here:
What is the definition of an absolute URL (fully qualified?)
(7 answers)
Closed 5 years ago.
I am trying to go to external url from my website with this code
var embed = "www.youtube.com";
console.log(embed);
window.location.assign(embed);
However, the webpage doesn't go to the the link in var embed but go instead to
Mywebsite/thepageofthatcode/www.youtube.com
window.location = "www.youtube.com";
window.location.href = "www.youtube.com";
I didn't get why is this happening?
Please ensure you add the http for external websites:
window.location.href = "http://www.youtube.com";
You should use the protocol before the url. Otherwise, the browser will think that it is a path.
var embed = "http://www.youtube.com";
console.log(embed);
window.location.assign(embed);
Add the protocol to make it work. Without the protocol it will search for www.youtube.com under your domain which is why it is redirecting to that way. Try
window.location.href = 'https://www.youtube.com';
Add the protocol to make it work. Without the protocol, it thinks that www.youtube.com is a part of your website.
This is the problem. But here's a better why to fix it:
// Use RegExp to test if embed already has the protocol
// If not, prepend it to embed.
if (!/^https?:\/\//i.test(embed))
embed = 'http://' + embed;
window.location.href = embed;
RegExp guide
URL Syntax
This solves the problem, only need to add the http part to the url
var embed = "http://www.youtube.com";
window.open(embed);
This question already has an answer here:
how to make Correctly a javascript if exist (folder) command?
(1 answer)
Closed 8 years ago.
Hello stackoverflow users
I have been trying for some time to design a script.
In this script I will look for commands with an if query whether a folder exists.
I do this by using a variable make so shall he find the path using the variable.
So as follows "backgrounds /" + variable;
Here's my script:
var mapname = "dolls";
$.get( "backgrounds/" + mapname )
.done(function() {
var eld = mapname ;
}).fail(function() {
var eld = "default";
})
I'm using JQuery version 1.3.2.
I get the following error in the JS Console
Uncaught TypeError: Object #<XMLHttpRequest> has no method 'done'
does anyone know how I write this code right?
thanks ahead...
Your code will attempt to do a GET-request to the URL you specified, which doesn't necessarily mean that it is a folder. If you try this on your local machine without a web server and provide a folder name, the browser will look for "index.html" or "index.htm" in that folder. If it finds one, it will succeed, which is not really what you're after..
About actual filebrowsing. Javascript does not allow file browsing for security purposes.
JQuery 1.3 ajax GET request would look something like this
var mapname = "dolls";
var eld;
jQuery.ajax({
type: "GET",
url: "backgrounds/" + mapname,
success: function(response) {
eld = mapname;
},
error: function(msg) {
eld = "default";
}
});
Here's the Fiddle
I am creating a webapp and I have been using tag in my JSPs to ensure that all my links (both to pages and to resources such as images/css) are always consistent from the root of the application, and not relative to my current location.
Some of the content I am creating using jQuery, for example, I am creating a HTML table by parsing a JSON object and using jquery.append() to insert it in to a div.
My question is, if I want to dynamically create a link using jquery how can I achieve a consistent URL regardless of the page being executed? I have tried just building the html with the tag in it, but no joy.
Thanks!
var baseURL = "/* Server-side JSP code that sets the base URL */";
$("<a />", { href: baseURL+"/my/resource/here.jsp" }); //Your proper link
Or you could do:
var baseURL = "http://"+location.host+"/my/base/url/";
//Which gives you something like http://mySite.com/my/base/url/
Get the root value of your webapp into a string using a jsp tag inside your javascript.
var root = < %=myRootVariable%> //This evaluates to http://www.myapp.com
var dynamicBit = "/foo/bar"
var dynamicLinkUrl = root + dynamicBit
var $newa = $("Hello, world");
$someJQElement.append($newa)
Hopefully none of this will occur in the global namespace. Just sayin'
This question already has answers here:
How to extract the hostname portion of a URL in JavaScript
(15 answers)
Closed 3 years ago.
function detailed_link(cell_rowid) {
var $tabs = $('#infrTab').tabs();
$tabs.tabs('select', 1); // switch to third tab~
objRowData = $('#' + pageGridId).getRowData(cell_rowid);
//document.getElementById("Name").value = objRowData.amount;
loadPage('Infringement/TaskDetail', 'taskDetails'); /* Path */
}
I have write a javascript function loadPage(), that needs a path to some page as a parameter. I need to give this path from the application root. I dont want a relative path. Please let me know how can I do it.
I have this piece of Javascript in my Site.master, just underneath the jquery import and above any reference to my own scripts.
<script type="text/javascript">
//Function that provides a relative URL for cases of virtual directories.
jQuery.url = function (path) {
return '<%: Url.Action("Index","Home") %>' + path;
};
</script>
This assumes that your '/' address is handled by your Index method in your Home controller (the standard).
You can then access it via:
$.url('Infringement/TaskDetail')
suppose that you have a page with this address: http://sub.domain.com/page.htm. use the following in page code to achive those results:
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.origin : you'll get http://sub.domain.com
I recommend to use relative path, because you never know where will be application root. If your application will be installed with another applications in IIS, then its root could be for example http://www.domain.com/iis/youapp/pages/one.aspx