简体   繁体   English

超链接中的当前页面URL

[英]Current Page URL in a hyperlink

In the code below, I am hard coding the url. 在下面的代码中,我正在对URL进行硬编码。

<a class="button_link" href="https://somewebsite.com/submit/?url=http%3A%2F%2Fsomecrazyurl.com" target="_blank" aria-label="Sharei">

Instead I want something like this: 相反,我想要这样的东西:

<a class="button_link" href="https://somewebsite.com/submit/?url=returnpageurl()" target="_blank" aria-label="Sharei">

Edit: For the record I used 编辑:我使用的记录

$(location).attr('href');

However nothing gets returned. 但是什么也没有返回。

Is there any cross-browser Javascript to return the current url of the page? 是否有跨浏览器Javascript返回页面的当前URL?

To get the path, you can use: 要获取路径,可以使用:

  var pathname = window.location.pathname; // Returns path only
  var url      = window.location.href;     // Returns full URL

You can use jQuery's attribute selector for that. 您可以为此使用jQuery的属性选择器。

var linksToGoogle = $('a[href="http://google.com"]');

Alternatively, if you're interested in rather links starting with a certain URL, use the attribute-starts-with selector: 另外,如果您对以某个URL开头的链接感兴趣,请使用attribute-starts-with选择器:

var allLinks = $('a[href^="http://google.com"]');

If you are looking for a browser compatibility solution use 如果您正在寻找浏览器兼容性解决方案,请使用

window.location.href 

where document.URL is having issues with Firefox with reference to this 此相关document.URL在Firefox中出现问题的地方

<a class="button__link" href="#" target="_blank" aria-label="Sharei" id="current_url">Current URL</a>

This is simple to use as below with no complexity ,the thing what I found is you are not assigning any value to the href attribute and by default in jquery it assigns back 这很容易使用,如下所示,没有任何复杂性,我发现您没有为href属性分配任何值,默认情况下,它在jquery中将其分配回

https://somewebsite.com/submit/?url=returnpageurl() https://somewebsite.com/submit/?url=returnpageurl()

Now the below one should work for your case, 现在,下面的一种应该适合您的情况,

 $("#current_url").attr("href",window.location.href );

This is easy to achieve by using the window.location.href JavaScript window property. 使用window.location.href JavaScript窗口属性很容易实现。

Html Example: HTML示例:

<a href="https://somepage.com" id="link1">my link</a>

Javascript Java脚本

var a = document.getElementById('link1');
a.href = a.href + '?returnUrl=' + encodeURIComponent(window.location.href);

With jQuery: 使用jQuery:

$('#link1').attr('href', $('#link1').attr('href') + '?returnUrl=' + encodeURIComponent(window.location.href));

Note the use of the built in function encodeURIComponent(str) reference. 注意内置函数encodeURIComponent(str) 参考的使用。

The encodeURIComponent() function encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters). encodeURIComponent()函数通过将表示字符的UTF-8编码的一个,两个,三个或四个转义序列替换某些字符的每个实例来编码统一资源标识符(URI)组件(对于字符而言,将仅是四个转义序列)由两个“代理”字符组成)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM