开发者

Converting relative paths to absolute with JavaScript

HTML:

<a href="/">1</a> // link to http://site.com
<a href="/section/page/">2/a> // link to http://site.com/section/page/
<a href="http://site.com/">3</a>
<a href="../gallery/1/">4</a> // link to http://site.com/gallery/1/

JS:

$("a").live('click', function(){
    var url = 开发者_如何学Python$(this).attr("href");
    //do something
});

How to convert relative path (var url) to absolute by jQuery?

Script should do nothing, if it is already an absolute path.

Thanks.


I'm pretty sure that if you use the href property instead of getting the attribute, you'll have a full url with domain:

$("a").live('click', function(){
    var url = this.href;    // use the property instead of attribute
    //do something
});

As noted in the question linked by @Phrogz, it sounds as though there are issues with IE6.

If you need to support it, you may need to build the href from the different parts like this.host and this.pathname. Those properties are supported by IE6. There are others you could use too, but you'd need to verify support.

jquery live() function deprecated in version 1.7 and removed from 1.9 so use alternate on():

$("a").on('click', function(){
    var url = this.href;    // use the property instead of attribute
    //do something
});


Not what the OP asked, but if anyone is trying to do this for <img> tags (as I was when I found this Question), the secret it not to use jQuery's attr method.

This gives you straight contents of the src attribute (which if it's relative, will be relative):

$('#your_img').attr('src')

Whereas calling .src on the DOM object itself always gives you the absolute path (what I needed):

$('#your_img').get(0).src


you can use jquery mobile

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery.mobile.path.makeUrlAbsolute demo</title>
  <link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
  <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  <style>
  #myResult{
    border: 1px solid;
    border-color: #108040;
    padding: 10px;
    }
  </style>
</head>
<body>

  <div role="main" class="ui-content">
    <p>The absoulte URL used is http://foo.com/a/b/c/test.html</p>
    <input type="button" value="file.html" id="button1" class="myButton" data-inline="true">
    <input type="button" value="../../foo/file.html" id="button2" class="myButton" data-inline="true">
    <input type="button" value="//foo.com/bar/file.html" id="button3" class="myButton" data-inline="true">
    <input type="button" value="?a=1&b=2" id="button4" class="myButton" data-inline="true">
    <input type="button" value="#bar" id="button5" class="myButton" data-inline="true">
    <div id="myResult">The result will be displayed here</div>
  </div>
<script>
$(document).ready(function() {

   $( ".myButton" ).on( "click", function() {

      var absUrl = $.mobile.path.makeUrlAbsolute( $( this ).attr( "value" ), "http://foo.com/a/b/c/test.html" );

    $( "#myResult" ).html( absUrl );
 })
});
</script>

</body>
</html>

Reference: https://api.jquerymobile.com/jQuery.mobile.path.makeUrlAbsolute/


The modern way is:

const url = new URL('MyPage.aspx', location)

Which will turn the page into an absolute url object. Works even with '/MyPage.aspx' or '../MyPage.aspx' or '../SiblingFolder/MyPage.aspx'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜