1

数ページ(http://something.com)に次のURLがあります。

<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>

そして私が欲しいのはすべてのリンクを...に変換することです

<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>

"#"だから基本的に私は変換や"../"そのようなリンクをしたくありません。

私はJSの初心者です。

私の努力から、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>

そして、私は何もできません...助けてください、どうすればURLを変更して追加できますかhttp://this.com/?url=My_web_page_url_here

更新 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>

それでも、すべてのURLは追加なしで同じようになります。

4

2 に答える 2

2

これを試して...

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

パラメータをエンコードしましたが、例のようにエンコードしたくない場合は、への呼び出しをドロップしますencodeURIComponent()

于 2012-11-25T10:08:15.633 に答える
0

これにアプローチするもう 1 つの方法は、イベント委任を使用することです。このメソッドは、リンクが使用される時点で(前ではなく) 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)
  }

}

また、次の関数を作成して、現在の に基づいて href を絶対値に拡張しますwindow.location。どのブラウザーが絶対 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;
}

実際の例 (リダイレクトの代わりにアラートを使用):

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

この方法の利点は、実行時に新しい html コンテンツを生成するアプリケーションで機能することです。これは、AJAX を利用したシステムで最もよく発生します。さらに、リンクは引き続きユーザーにオリジナルとして表示されます(これは、何をしているかによっては望ましい場合があります)

于 2012-11-25T10:36:53.793 に答える