Resolve relative Urls from Javascript (for ASP.NET)


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Url = function() { }
  2.  
  3. Url.prototype =
  4. {
  5. _relativeRoot: '<%= ResolveUrl("~/") %>',
  6.  
  7. resolve: function(relative) {
  8. var resolved = relative;
  9. if (relative[0] == '~') resolved = this._relativeRoot + relative.substring(2);
  10. return resolved;
  11. }
  12. }
  13.  
  14. $Url = new Url();
  15.  
  16.  
  17. //Now, anywhere I need to resolve a relative url in a script, I can do this:
  18. var logoUrl = $Url.resolve("~/Images/logo.png");

URL: http://iridescence.no/post/Resolving-relative-URLe28099s-from-JavaScript.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.