简体   繁体   English

如何通过javascript将所有href链接附加到重定向器url

[英]How to append all href link with redirector url via javascript

I have following URL in my few pages(http://something.com)..我的几页中有以下 URL (http://something.com)..

<a href="http://something.com">Home</a>
<a href="index.html">Index</a>
<a href="about.html">About</a>
<a href="contact.php">Contact</a>
<a href="#">Same</a>
<a hre="http://example.com/home.html">New Home</a>
<a href="../services.html">Services</a>

and what I want is to convert all link to...我想要的是将所有链接转换为...

<a href="http://this.com/?url=http://something.com">Home</a>
<a href="http://this.com/?url=http://something.com/index.html">Index</a>
<a href="http://this.com/?url=http://something.com/about.html">About</a>
<a href="http://this.com/?url=http://something.com/contact.php">Contact</a>
<a href="#">Same</a>
<a hre="http://this.com/?url=http://example.com/home.html">New Home</a>
<a href="../services.html">Services</a>

So Basically I don't want to Convert "#" or "../" such link.所以基本上我不想转换"#""../"这样的链接。

I am noob in JS.我是 JS 的菜鸟。

From my effort, with the help of w3schools.. What i have tried to accomplish :-通过我的努力,在 w3schools 的帮助下.. 我试图完成的事情:-

<script type="text/javascript">
var url= document.getElementByTagName(a);
var pattern = /..\//g;
var result = pattern.test(url);
if(url.href != "#" || result === "false" ) {
var nurl = url.replace("http://this.com/?url="+url.href, url.href);
}
</script>

And I am not able to do anything... Please help, how can I modify URL and add http://this.com/?url=My_web_page_url_here .而且我什么也做不了...请帮忙,我如何修改 URL 并添加http://this.com/?url=My_web_page_url_here

UPDATE I replaced my javascript with更新我用我的javascript替换了

<script type="text/javascript">
var links = document.links;
var i = links.length;

while (i--) {
    if (links[i].href.slice(-1) == "#" || 
        links[i].getAttribute("href").slice(0, 3) == "../") {
        continue;
    }
    links[i].href = "http://this.com/?url=" + encodeURIComponent(links[i].href);
}​
</script>

And still all url's are in the same way without append.并且所有 url 的方式都相同,没有附加。

Try this...尝试这个...

var links = document.links;
var i = links.length;

while (i--) {
    if (links[i].href.slice(-1) == "#" || 
        links[i].getAttribute("href").slice(0, 3) == "../") {
        continue;
    }
    links[i].href = "http://this.com/?url=" + encodeURIComponent(links[i].href);
}​

jsFiddle . js小提琴

I encoded the parameter, but if you don't want it encoded, like in your examples, drop the call to encodeURIComponent() .我对参数进行了编码,但如果您不希望对其进行编码,就像在您的示例中一样,请放弃对encodeURIComponent()的调用。

Another way to approach this is with event delegation.解决此问题的另一种方法是使用事件委托。 This method rewrites the URL at the point the link is used (and not before) :此方法在使用链接时(而不是之前)重写 URL:

window.onload = function(){

  var de = document.documentElement || document.body;
  /// an event listener that steps in at the point the user has
  /// clicked any element on the page. We specifically detect for
  /// links and redirect the outcome
  var proxyLink = function(e,t,href){
    /// handle old versions of IE
    e = e || Event; t = e.target || e.srcElement;
    /// hashs can occur at the end of external links, so am only checking
    /// your specific case. Switched to using getAttribute so as to get
    /// the original href string.
    if ( t.getAttribute('href').charAt(0) === '#' ) return;
    if ( t.getAttribute('href').indexOf('../') === 0 ) return;
    if ( String(t.nodeName).toLowerCase() == 'a' ) {
      if ( e.preventDefault ) { e.preventDefault(); }
      href = makeHrefAbsoluteIfPossible(t.href);
      window.location = 'http://this.com/?url='+encodeURIComponent(href);
      return (e.returnValue = false);
    }
  }

  /// add the listener to the body or document element, this will trigger
  /// the event onclick thanks to event bubbling.
  if ( de.addEventListener ) {
    /// handles modern browsers
    de.addEventListener('click', proxyLink, true);
  }
  else {
    /// handles old IE
    de.attachEvent('onclick', proxyLink)
  }

}

I've also create the following function to extend your hrefs to their absolute value based upon the current window.location .我还创建了以下函数,根据当前window.location将您的 href 扩展到它们的绝对值。 I'm never sure which browsers will return an absolute URL or the original href text, so this function is just incase the latter happens:我永远不确定哪些浏览器会返回绝对 URL 或原始 href 文本,所以这个函数只是为了防止后者发生:

function makeHrefAbsoluteIfPossible( href ){
  var path;
  if ( href.indexOf(':') == -1 ) {
    if ( href.charAt(0) == '/' ) {
      path = href;
    }
    else {
      path = String(window.location.pathname);
      if ( path.indexOf('/') != -1 ) {
        path = path.substring(0,path.lastIndexOf('/')) + '/';
      }
      path += href;
    }
    return window.location.protocol +'://' +
      window.location.host + path;
  }
  return href;
}

Working example (with an alert instead of a redirect):工作示例(使用警报而不是重定向):

http://jsfiddle.net/teykz/2/ http://jsfiddle.net/teykz/2/

The benefits to this method are that it will work with applications that generate new html content during runtime... this most often happens with AJAX powered systems.这种方法的好处是它可以与在运行时生成新 html 内容的应用程序一起使用......这最常发生在 AJAX 驱动的系统中。 Plus the links still appear as the original to the user (this can be desired depending on what you are doing) .此外,链接仍然作为原始链接出现在用户面前(这取决于您正在做什么)

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

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